• SJW

    (@whitsey)


    I am using the user_register hook to assign all new users a specific role.

    This assigns the user role to everyone. Is there a way I can differentiate a backend user_register from a frontend user_register?

Viewing 2 replies - 1 through 2 (of 2 total)
  • narolainfotech

    (@narolainfotech)

    Hello,
    I appreciate your question, well

    Here’s an example of how you can achieve this by using the is_admin() function:

    function custom_user_register($user_id) {
       
        if (is_admin()) {
           echo "hello";
        } else {
            $role_to_assign = 'your_frontend_role';
            wp_update_user(array('ID' => $user_id, 'role' => $role_to_assign));
        }
    }
    
    add_action('user_register', 'custom_user_register');
    

    Adjust the code according to your specific needs and the roles you want to assign. you can set value instead of your_frontend_role in above code like admin, editor any role you want, just add there.
    Hope you find your answer. Thank you

    Moderator bcworkz

    (@bcworkz)

    Using is_admin() was my initial thought as well. But it might not be effective, depending on how front end forms manage registration. It’s possible for a front end form to register by making an admin request, in which case checking is_admin() would be ineffective.

    It might be necessary to check the referrer page; or alter the front end form to include a hidden field so your registration callback can easily distinguish front end registration requests.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to assign a user role on frontend registration only’ is closed to new replies.