add_action and set_role (6 posts)

  1. Fairweb
    Posted 6 years ago #

    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.

  2. Steven
    Posted 6 years ago #

    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;
      $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)
  3. Steven
    Posted 6 years ago #

    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?

  4. Fairweb
    Posted 6 years ago #

    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 :)

  5. fazalmajid
    Posted 5 years ago #

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

  6. Fairweb
    Posted 5 years ago #

    Thanks, I'll check on that.

Topic Closed

This topic has been closed to new replies.

About this Topic