0

I am new to wordpress I want to send email notifications for commenting in blogs. Currently it is going to only one user. I tried 'comment notifier' plugin but got negative result. I tried the below code

add_filter('comment_notification_recipients', 'override_comment_notice_repicient', 10, 2);
function override_comment_notice_repicient($emails, $comment_id) {
    $comment = get_comment( $comment_id );
    if ( empty( $comment ) )
        return $emails;
    $post = get_post( $comment->comment_post_ID );

    return array('[email protected]');
}

but it isn't working. I googled it but couldn't get anything. Please help me solve it.

3
  • HI Rishi, Welcome. The code should return the array of emails to which you wish notifocations to go. Currently you appear to be returning one email address? Note also that the code does not need to check empty comment etc. That is done in wp. See developer.wordpress.org/reference/functions/… where the filter is applied. Commented Aug 17, 2018 at 6:45
  • @anmari Thanks for the reply. But the reference you gave is for sending notifications to the post author. I need to send it to other admins. Commented Aug 17, 2018 at 7:16
  • Wordpress sends to the author by default, but gives the filter you mentioned above so you can add to the list of email addresses. Look at the link, scroll down, open the source code and find the where it says "Filters the list of email addresses to receive a comment notification. * * By default, only post authors are notified of comments. This filter allows * others to be added." Commented Aug 17, 2018 at 7:19

1 Answer 1

1

Something like this should work:

 add_filter('comment_notification_recipients', 'override_comment_notice_repicient', 10, 2);
    function override_comment_notice_repicient($emails, $comment_id) {  
        $admins = get_users( array(
            'role__in'     => array('administrator'),
        ) );
        foreach ( $admins as $user ) {
            $emails[] =  $user->user_email;
        }
        return ($emails);
    }
4
  • 1
    it is not working. I used die('testing') inside the function. it is not even entering the function. Are there some other issues which I need to solve? Commented Aug 17, 2018 at 8:47
  • It's working for me. I added error_log calls as per below. The function is entered & array of emails returned. Check your error_log location to see the output (better than die, with die, the output may be trapped not seen, and of course, the result is not passed through and action not taken. '.. error_log("in Function"); $admins = get_users( array( 'role__in' => array('administrator'), ) ); foreach ( $admins as $user ) { $emails[] = $user->user_email; } error_log(print_r($emails,true)); return ($emails); ..' Commented Aug 17, 2018 at 10:21
  • I even added an error_log in the wp function where it sends to each email. Line 1563 in the code of developer.wordpress.org/reference/functions/… and it's definitely getting there ;) Had all my admin users listed there. Commented Aug 17, 2018 at 10:34
  • A good way to test with emails on your localhost is using the tool referred to here stackoverflow.com/questions/5773288/… Commented Aug 17, 2018 at 10:35

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.