0

I am currently building a wordpress plugin to send email notification to list of users roles who comments on a post.

For Example:

If a user with "subscriber" role comments on a post all the other users who have "subscriber" role gets email alert.Currently i have achieved this through the following code

if( current_user_can('subscriber'))
{
$user_query = new WP_User_Query( array( 'role' => 'subscriber');
                foreach ( $user_query->results as $user ) 
                {
                    $mail=$user->user_email;


                    $subject  ="this is example";
                    $message  = "";
                    $message .= "$post_permalink\n";

                    wp_mail($user->user_email,  $subject,$message);         
}
}

The above code sends email to all the users who have "subscriber" role.

Output:

It gets even the current user who comments on the post and send the email notification.

Problem to be solved:

How can i exclude the current user email and send only notification to other users those who have "subscriber" role .

2 Answers 2

1

First, you should know that current_user_can() only accepts capabilities, not roles, so you are using it wrong and you can end up with unexpected results.

That being said, to exlude users form WP_User_Query you can use the exclude parameter:

// Get current user data
$user = wp_get_current_user();

// Check if current user is a subscriber
if( ! empty( $user ) && in_array( "subscriber", (array) $user->roles ) ) {

    $user_query = new WP_User_Query( array(
                                           'role'   => 'subscriber',
                                           // exlude current user
                                           'exclude' => array( $user->ID ),
                                     )
    );

    $subscribers = $user_query->get_results();

    if( ! empty( $subscribers ) ) {
        foreach ( $subscribers as $user )  {

            $mail = $user->user_email;
            $subject  ="this is example";
            $message  = "";
            $message .= "$post_permalink\n";

            wp_mail( $mail, $subject, $message);

        }
    }

}
-1

Following should works for you:-

 if( current_user_can('read')) { 
    $exclude_user = array();
    $exclude_user[] =get_current_user_id();
    $args = array(
    'role' => 'subscriber',
    'exclude' => $exclude_user
    );
    $user_query = new WP_User_Query( $args); 
    foreach ( $user_query->results as $user ) { $mail=$user->user_email;

                    $subject  ="this is example";
                    $message  = "";
                    $message .= "$post_permalink\n";

                    wp_mail($user->user_email,  $subject,$message);         
    }


}
7
  • 1
    Using current_user_can() function to check roles is an old practice not recommended any more (at least since 3 years ago). In fact, the recomendation talks only abour capabilities. If you use current_user_can() with roles, you can end up with unexpected results.
    – cybmeta
    Commented Aug 3, 2015 at 10:12
  • Wisdmlabs the above code is not working for me Commented Aug 3, 2015 at 10:14
  • cybmeta I dont have problem in checking the user role i have problem in fetching the values of user with "subscriber" role. It get current user email also.. i need to avoid fetching it.. Commented Aug 3, 2015 at 10:18
  • I didn't say you have a problem with that, I said that you can end up with problems because you are using current_user_can incorrectly. That comment was not intended to answer your question but provide an advise.
    – cybmeta
    Commented Aug 3, 2015 at 10:20
  • We have just given the approach for fetching users with role as subscriber excluding current user id.
    – Domain
    Commented Aug 3, 2015 at 10:27

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.