最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to change the email notification recipient (user) for new comments?

programmeradmin4浏览0评论

How do you change the user that gets the notification email announcement for new comments and comment moderation?

WordPress sends the notices to the admin user. My client is the editor of the site. I want the comment notices to get mailed to the editor user and not the admin user.

How do you do that?

How do you change the user that gets the notification email announcement for new comments and comment moderation?

WordPress sends the notices to the admin user. My client is the editor of the site. I want the comment notices to get mailed to the editor user and not the admin user.

How do you do that?

Share Improve this question edited Sep 30, 2020 at 7:02 Jesse Nickles 7357 silver badges19 bronze badges asked May 3, 2016 at 0:44 user93385user93385 811 gold badge1 silver badge2 bronze badges 1
  • Did either answer help you? If so then please consider accepting one. – Andy Macaulay-Brook Commented Sep 20, 2016 at 12:42
Add a comment  | 

5 Answers 5

Reset to default 11

There's a great article explaining how to hook into 2 filters for this at https://web.archive/web/20200216075253/http://www.sourcexpress/customize-wordpress-comment-notification-emails/

To send your notifications to a particular user and not the site admin, try this for a user with ID 123:

function se_comment_moderation_recipients( $emails, $comment_id ) {
    $comment = get_comment( $comment_id );
    $post = get_post( $comment->comment_post_ID );
    $user = get_user_by( 'id', '123' );

    // Return only the post author if the author can modify.
    if ( user_can( $user->ID, 'edit_published_posts' ) && ! empty( $user->user_email ) ) {
        $emails = array( $user->user_email );
    }

    return $emails;
}
add_filter( 'comment_moderation_recipients', 'se_comment_moderation_recipients', 11, 2 );
add_filter( 'comment_notification_recipients', 'se_comment_moderation_recipients', 11, 2 );

I'm not aware of any hook that could change only the comment notification recipient... You would probably need to overwrite some kind of core function, but here's a small workaround you could use:

1. Disable the email feature from WordPress comments settings (unless you want to get notified too)

2. Send it manually using the comment_post action hook. Just add this function to functions.php

    add_filter( 'comment_post', 'comment_notification' );

    function comment_notification( $comment_ID, $comment_approved ) {

        // Send email only when it's not approved
        if( $comment_approved == 0 ) {

            $subject = 'subject here';
            $message = 'message here';

            wp_mail( '[email protected]' , $subject, $message );
        }
    }

    // Remove if statement if you want to recive email even if it doesn't require moderation

`comment_post` is an action triggered immediately after a comment is inserted into the database.

There is a filter for changing the text of the comment moderation email:

function change_comment_email( $body, $comment_id ) {
    $body = preg_replace( "/(A new )comment/s",  "$1review", $body );
    $body = preg_replace( "/(Currently \d+ )comment/s",  "$1review", $body );
    $body = preg_replace( "/Comment:/",  "Review:", $body );
    return $body;
}

add_filter( 'comment_moderation_text', 'change_comment_email', 20, 2 );
add_filter( 'comment_notification_text', 'change_comment_email', 20, 2 );

If you want to send this email to multiple people then use below code in functions.php -

function se_comment_moderation_recipients( $emails, $comment_id ) {

    $comment = get_comment( $comment_id );
    $post = get_post( $comment->comment_post_ID );
    $emails = array();
   // $users =  array(set of user IDs t whome you want to send mails)
    $users =  array( 1, 16628, 15983 );
    foreach($users as $uid){
    $user = get_user_by( 'id', $uid );
    // Return emails of users .
         if ( !empty( $user->user_email ) ) {
           $emails[] =  $user->user_email;
        }
    }
    $emails_list = array(implode(",",$emails));
     return $emails_list;

}
add_filter( 'comment_moderation_recipients', 'se_comment_moderation_recipients', 11, 2 );
add_filter( 'comment_notification_recipients', 'se_comment_moderation_recipients', 11, 2 );

An alternative and more flexible way to the code hacking in the other answers is:

Create an email address for the admin account of the blog. For example, [email protected] that is different from [email protected] and [email protected].

Option A: Forward site@ email to both the editor and the technical admin. I create site@ as an alias. This works if your editor is okay with receiving a copy of all the automatic email generated by the site. They just filter irrelevant email out or learn what actually happens with a site. This is good for small clients.

Option B: Set up a mail filter for site@ to automatically forward emails about comment alerts to the editor and all email to the technical admin. The technical admin can then filter to archive/delete all comment alerts so they never appear in their inbox. This initial forward to editor@ can be done on the mail server using something like procmail. Alternatively you can do it on your email client if it's up 24/7 or you can even use Gmail, Hotmail, etc. and manually build the filter.

发布评论

评论列表(0)

  1. 暂无评论