Change WordPress user roles and capabilities Forums New Feature Request What action fires when users are added to a new role from the bulk edit users

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #7425
    firstclasscode
    Participant

    Hello,

    I need to fire off some code for when users are added to a new role, but not when editing the profile directly. I need to fire the code off when users are added to a new role from the main “all users” screen – when using the bulk add to role function.

    Basically we want to send an email to the user once they have been added to a new role. I have code that can do this if the user is created in that role, but not sure how to hook into adding the code to when they are added as secondary using bulk edit function..

    Thanks,
    -Reece

    #7426
    Vladimir
    Keymaster

    Hi,

    URE uses WP_User::add_role() method (wp-includes/class-wp-user.php, #536 (WP v. 5.7) in this case, which fires this action

    
    do_action( 'add_user_role', $this->ID, $role );
    

    Check current page and additional parameters to execute your own hook only for the “Users” page and “Add Role” bulk action, not for other cases. For example, URE itself checks this:

    
    if ( ( !empty( $_REQUEST['ure_add_role'] ) && !empty( $_REQUEST['ure_add_role_submit']) ) || ( !empty( $_REQUEST['ure_add_role_2'] ) && !empty( $_REQUEST['ure_add_role_submit_2'] ) ) ) {
    // do something ...
    
    #7427
    firstclasscode
    Participant

    Vladimir,

    Perfect, that works exactly as I needed it to.

    Thanks!

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.