• Hi,
    I’m not able to make this functions.php function work.
    All new user registrations are “subscribers”. Inside the site they ask to be a “seller”.

    When approved as “Seller” it is automatically approved as “Manager

    add_action( ‘user_register’, ‘add_secondary_role’, 10, 1 );
    function add_secondary_role( $user_id ) {
    $user = get_user_by(‘id’, $user_id);

    if (userpro()) {
    $user_id->userpro = true;
    $user->add_role(‘manager’);
    }
    }

    Some help?

    • This topic was modified 1 year, 10 months ago by Jan Dembowski.
Viewing 6 replies - 1 through 6 (of 6 total)
  • The functions.php is in a child theme directory and the child theme is the active theme?

    Thread Starter kklo

    (@kklo)

    Hi, @threadi

    Yes.
    There are two plugins: Tutor LMS and WCFM. The default user registration is “Subscriber”. When he wants to be an “Instructor” he asks to be an “Instructor” and the “Admin” authorizes it. In this authorization, he automatically changes to “Subscriber” and “Instructor (tutor_instructor)” and at this moment I want him to also be “Manager (shop_manager)”

    You should first check if the functions.php of your child theme is working at all. Insert this at the end of the file:

    add_action( 'init', function() { echo "Hello World";exit; });

    If you then call any page of your web you should get “Hello World”. If this works, then the functions.php is definitely used. Remove this line after the test.

    So if this works the question would be why your entry does not work. The hook “user_register” only works for new users: https://developer.wordpress.org/reference/hooks/user_register/

    As you describe it, you want to adjust an existing user. You should do that via the hook https://developer.wordpress.org/reference/hooks/set_user_role/.

    Thread Starter kklo

    (@kklo)

    Hi,

    Yes it’s working.
    I tried to do another function and it’s not working either.

    The user is not new, it can often be an already registered user who decided to become an instructor.

    Can anyone help me where is the error?

    function add_wcfm_vendor_role_to_tutor_instructor( $user_id ) {
    $user = new WP_User( $user_id );
    $roles = $user->roles;
    if ( in_array( 'tutor_instructor', $roles ) ) {
    $user->add_role( 'wcfm_vendor' );
    }
    }
    add_action( 'tutor_instructor_approved', 'add_wcfm_vendor_role_to_tutor_instructor' );


    • This reply was modified 1 year, 10 months ago by kklo.

    The hook “tutor_instructor_approved” means nothing to me. Where did you get it? You can check if it works by test outputs in this. Add e.g. directly at the beginning

    echo "aaaa";exit;

    If you then set the permissions and don’t see “aaaa” in the browser, this hook obviously doesn’t work.

    I would rather use set_user_role as written above.

    Thread Starter kklo

    (@kklo)

    Hi @threadi

    Yes, functions.php is working perfectly, I’ve already tested it.
    The code above was another attempt to create a function that added one more role to “tutor_instructor”

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Functions.php’ is closed to new replies.