0

I would like an email to be sent to the user when their role is changed to Author.

The code below sends an email when a user's role is changed to any role, but I would like it only to send if the role is changed to 'Author'.

function user_role_update( $user_id, $new_role ) {
    $site_url  = get_bloginfo( 'wpurl' );
    $user_info = get_userdata( $user_id );
    $to        = $user_info->user_email;
    $subject   = 'Role changed: ' . $site_url . '';
    $message   = 'Hello ' . $user_info->display_name . ' your role has changed on ' . $site_url . ', congratulations you are now an ' . $new_role;
    wp_mail( $to, $subject, $message );
}
add_action( 'set_user_role', 'user_role_update', 10, 2 );

1 Answer 1

1

Check for the value of $new_role before sending the email, if it isn't author then do nothing

function user_role_update( $user_id, $new_role ) {
    if ( $new_role == 'author' ) {
            $site_url  = get_bloginfo( 'wpurl' );
            $user_info = get_userdata( $user_id );
            $to        = $user_info->user_email;
            $subject   = 'Role changed: ' . $site_url . '';
            $message   = 'Hello ' . $user_info->display_name . ' your role has changed on ' . $site_url . ', congratulations you are now an ' . $new_role;
            wp_mail( $to, $subject, $message );
    }
}
add_action( 'set_user_role', 'user_role_update', 10, 2 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.