1

I need to run a query when the admin changes the user role in the admin panel. Right now, this is my code:

add_action( 'set_user_role', 'my_function' ); function my_function ($user_id){ $user = new WP_User( $user_id ); global $wpdb; $wpdb->query(" "); }

This is not working. So my question is if this is the right hook to run a query when user vendor is updated. If yes, am I getting the right user id?

2

You are going in wrong direction there is no way to get user role while admin change from back end. You can retrieve the role object for the current user by calling get_role(), but that's an object rather than a string with the role name.

function get_user_role($uid) {
global $wpdb;
$role = $wpdb->get_var("SELECT meta_value FROM {$wpdb->usermeta} WHERE meta_key = 'wp_capabilities' AND user_id = {$uid}");
  if(!$role) return 'non-user';
$rarr = unserialize($role);
$roles = is_array($rarr) ? array_keys($rarr) : array('non-user');
return $roles[0];
}

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.