Menü schliessen
Created: August 2nd 2021
Last updated: August 2nd 2021
Categories: Wordpress
Author: LEXO

Changing comment notification Email recipient and customize content

Tags:  ACF,  comments,  functions.php,  PHP,  wordpress
Donation Section: Background
Monero Badge: QR-Code
Monero Badge: Logo Icon Donate with Monero Badge: Logo Text
82uymVXLkvVbB4c4JpTd1tYm1yj1cKPKR2wqmw3XF8YXKTmY7JrTriP4pVwp2EJYBnCFdXhLq4zfFA6ic7VAWCFX5wfQbCC

There is an easy way to change Email recipient for new comment notification.

By default Email is sent to Administration Email Address set under Settings > General. If there is a need to change that so it is not sent to Administrator but to content editor, or any other Email, it can be customized by pasting this code in function.php

add_filter( 'comment_post', 'comment_notification' );
function comment_notification( $comment_ID, $comment_approved = 0 ) {
    if( $comment_approved == 0 ) {
        $headers = array('Content-Type: text/html; charset=UTF-8');
        $subject = 'New comment notification';
        $message = 'To review and approve comment you can check it here - '.get_bloginfo('url').'/wp-admin/edit-comments.php';
        wp_mail( 'editor@email.com' , $subject, $message, $headers );
    }
}

If you are using ACF and have Theme Options setup on page, we can put all of these hardcoded options into Theme Options, so we can change them from WordPress backend.
BONUS TIP: We can customize position of the link to backend "Comments edit" page by adding variable %link% in ACF field for message content, and then replace it in code with actual link.

add_filter( 'comment_post', 'comment_notification' );
function comment_notification( $comment_ID, $comment_approved = 0 ) {
    if( $comment_approved == 0 ) {
        $headers = array('Content-Type: text/html; charset=UTF-8');
        $subject = get_field('email_subject','options');
        $link_url = get_bloginfo('url').'/wp-admin/edit-comments.php';
        $message = str_replace('%link%', '<a href="'.$link_url.'">'.$link_url.'</a>' , get_field('comments_email_message','options') );
        wp_mail( get_field('email_recipient','options') , $subject, $message, $headers );
    }
}