• Is it possible to add an action when a role is set or created for a user ? I looked through the code, the forum and Google but could not find a way of doing it. set_role is obviously a method of WP_User. Is it possible to add an action to a method ?

    Thanks for your help.

Viewing 5 replies - 1 through 5 (of 5 total)
  • In my user registration, the user can them selves choose their role.
    But I have not been able to hook this up with user registration.

    I’m also not able to run my function from my plugin using add_action.

    So what I do now, is to call my function on page load (Above the header – before <!DOCTYPE>).

    Unfortunately the user have to reload the page to see the effect. I’m currently looking into how I can solve this problem, and change user role before anything else is loaded (that is role based).

    Here is my function:

    function changeUserRole()
    {
      global $current_user;
      get_currentuserinfo();
    
      $user_ID = $current_user->ID;
      $user = new WP_User( $user_ID );
      $roles = array(... list of roles...);
    
      // Set new user role.
      foreach($roles as $role => $name) {
        if( $current_user->role == $name)
          $user->set_role($role);
      }
    }

    PS. hmm… reading your Q again, this might not be the answer you are looking for 🙂

    Did you however find a solution for your problem?

    Thread Starter Fairweb

    (@fairweb)

    Thanks for answering me. I’m also changing users’ roles and capabilities on the fly in a similar way as the one you are using. However, this did not quite sort out my problem.

    I just wanted to add an action when an admin user changes any other user’s role through the core backend.

    If you have an idea…

    Thanks 🙂

    The action set_user_role would accomplish what you seek. It is called in wp-includes/capabilities.php

    Thread Starter Fairweb

    (@fairweb)

    Thanks, I’ll check on that.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘add_action and set_role’ is closed to new replies.