• Resolved pixelyzed

    (@pixelyzed)


    Hi!

    I was using Members on a new site I’m working on and, one of the features is that users can register and are added in the default subscriber role on registration (nothing out of the ordinary here). But, after that, following offline procedures, an admin will go in and change the user’s role from Subscriber to a custom role named “member” and, when they do I have a function tied to the set_user_role hook (https://codex.www.ads-software.com/Plugin_API/Action_Reference/set_user_role) that sends two emails if and only if the new role is “member”.

    One email goes to an admin email for confirmation and one to the user telling them their status has changed on the site.

    This worked great up to the 1.x version but now, this hook doesn’t seem to be firing at all since Members 1.x replaced the native user role drop down select field with a checkbox field to enable the selection of multiple roles. for a single user.

    Was that by design? If not I guess this is a bug ?? If it is by design, is there another hook I can use to do the same thing? Maybe a Members specific hook?

    Thanks! Love the new version otherwise ??

    https://www.ads-software.com/plugins/members/

Viewing 4 replies - 1 through 4 (of 4 total)
  • set_user_role has never been a reliable hook because it’s only called when a user’s roles are wiped and set to a single role. It’s called when WP_User::set_user_role() executes. WordPress has 3 different methods of changing roles:

    WP_User::add_user_role()
    WP_User::remove_user_role()
    WP_User::set_user_role()

    Prior to WP 4.3, add_user_role() and remove_user_role() had no action hooks. They do now (add_user_role and remove_user_role). You should be checking against all three of those hooks for changes.

    There are actually other hooks you should’ve used prior to WP 4.3 for reliability, but these are the best now.

    Thread Starter pixelyzed

    (@pixelyzed)

    Awesome Justin, thank you! I’ve tested the add_user_role hook locally and it seems to be working fine. I’m guessing it is called once for each role added right? Because the value of the $role argument seems to still be a string just like for set_user_role. The conditional logic for my function checks for that $role arg being equal to a specific string and that still works even when I set 2 new roles or more at once.

    Curious to know what other hooks might have worked for this prior to 4.3 but it doesn’t matter in this case as this is a brand new site that was all built on 4.3 and 4.3.1.

    Thanks again very much!

    I’ve tested the add_user_role hook locally and it seems to be working fine. I’m guessing it is called once for each role added right? Because the value of the $role argument seems to still be a string just like for set_user_role. The conditional logic for my function checks for that $role arg being equal to a specific string and that still works even when I set 2 new roles or more at once.

    Yes, it fires each time a new role is added. So, if you add multiple roles, it’ll fire multiple times. So, it just passes a string for the role:

    do_action( 'add_user_role', $user_id, $role );

    You’ll still want to use set_user_role too, by the way. Core WP uses set_user_role() on the Users screen in the admin. Other plugins can utilize that function as well.

    Curious to know what other hooks might have worked for this prior to 4.3 but it doesn’t matter in this case as this is a brand new site that was all built on 4.3 and 4.3.1.

    Probably the most reliable hook was update_user_meta.

    I’m going to sticky this topic since several people have asked about it.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Changing role no longer fires the set_user_role hook’ is closed to new replies.