5

How can I execute a function, when admin changes the user role of a user?

I have two user roles: one is agent and the other one is client .

My need is that when admin changes the user role of client to agent, I need to execute a specific function.

In this function I need to get the all content and meta fields of user by getting the user id.

9

You can use the set_user_role hook, that will only fire when the user role changes:

add_action( 'set_user_role', function( $user_id, $role, $old_roles ) 
{
    // Your code ...

}, 10, 3 );

If you want to restrict this to a profile update, you can use:

add_action( 'set_user_role', function( $user_id ) 
{
    add_action( 'profile_update', function( $user_id )
    {
        // Your code here ...            
    } );

} );
  • could you please explain what is 10, 3 in this? – ron r Jul 13 '15 at 12:59
  • 2
    yes sure, 10 is the priority and 3 is the number of input arguments. If we use 10, 2, then we don't get access to $old_roles. The default priority is 10 and the defulat # input arguments is 1. @ronr – birgire Jul 13 '15 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.