0

Im interested to know if it possible Assign a secondary role IF the user it register by a specific role.

For example:

If the user register as "OWNER" wordpress must assign the existing user role "TEACHER".

I found different funtions online but nobody works, so i was wondering this specific needed its not implemented by wordpress.

Thank you!

EDIT:

I try to manage by myself a code but still i have some issue to compare the current user role at the moment of the registration:

add_action( 'user_register', 'add_secondary_role', 10, 1 );

function add_secondary_role( $user_id ) {

    $user = get_user_by('id', $user_id);

if ( in_array( $roles, 'owner' ) ) {
    
    $user->add_role('teacher');

} else if ( in_array( $roles, 'guest' ) ) {
    
    $user->add_role('student');

}

}
8
  • Are you asking how to assign multiple roles? Or how to switch the role of a user? How would a user register as an owner?
    – Tom J Nowell
    Commented Nov 17, 2020 at 1:38
  • 1) yes 2) no 3)By registration form. User register by front-end ad OWNER or GUEST; if it register by OWNER i would like to add also the TEACHER user role, if not adding the TEACHER. At the end, both user must have multiple role. Commented Nov 17, 2020 at 3:06
  • And what problem does it solve? Is there a particular reason a new role cannot be created that has the capabilities of both the teacher and owner roles? Or are you using a plugin that has features for the teacher role and features for the owner role and you believe that having multiple roles will solve an unmentioned problem?
    – Tom J Nowell
    Commented Nov 17, 2020 at 11:43
  • I want to connect my themes roles to a plugin roles, who (obliviously) have different roles and capabilities. Its soo bad? Commented Nov 17, 2020 at 13:37
  • If those plugins are built right then its the capabilities the role has that they check for, not the roles themselves. While it is possible to have multiple roles for a user at the database level, most APIs are built on the assumption of a single role, no UI is available for multi-role users, and most plugin authors are unaware it's even possible, so may see the first role and ignore the second. The easiest path forward for you is probably that plugins support route
    – Tom J Nowell
    Commented Nov 17, 2020 at 15:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.