I am facing a strange issue. Some of my user roles are changing automatically. I searched in the whole project and cannot find the location from where user roles are changing. So I added the following code in my functions.php

add_action( 'set_user_role', function( $user_id, $role, $old_roles ){
    mail('[email protected]','User role is changed from admin',
         'This mail is sending from set_user_role hook in functions line 2450. The user id is '.$user_id.
         'The loggedin user is '.get_current_user_id()
}, 10, 3 ); 

I am getting the emails with changed userids. but the currently logged in user is showing as 0. So I assume the function is not calling from admin and the following function is running somewhere in my function.

$user->add_role( 'userrole' );

But I cannot find the function $user->add_role(); in my project. Is this hook is called in any other cases? Please help me to solve this.


1 Answer 1


Good catch with the action, catching the instance.

You might be able to add a debug_backtrace() to trace it back to the calling functions, and their calling functions, to see how you actually get there.


Your Answer

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

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