How to Change the Comment Notification Email in WordPress

How to Change the Comment Notification Email in WordPress

Whenever someone leaves a comment on a WordPress blog post, the system sends the post author a notification e-mail. We’ve already seen how to change the sending address when installing WordPress from a scripted hosted installation. But we can also change the body of the text itself using the inbuilt filters. Until 15 April 2014, WordPress used to ignore pagination while providing us with the comment permalink in the notification e-mails. The Automattic team fixed that in an update, but that doesn’t mean that there is no longer a need to add additional text or change the comment notification email in some way or the other. For example, let’s assume you want to add a legal or privacy notification or include some instructions for processing. Here’s how to implement it using a simple code placed into our functions.php file.

Using the comment_notification_text Filter

By default, WordPress includes some very basic information in the e-mail notification sent to the post author. The content of the comment, the link to the comments section, the permalinks to the comment itself, as well as the URLs for editing, deleting, and marking it as spam (if the requisite permissions are available to the post author of course).

default notification

The core WordPress file pluggable.php contains the function wp_notify_postauthor which accepts the comment ID as an argument and sends out the appropriate notification. Iit contains three pluggable functions:

  • comment_notification_text;
  • comment_notification_subject;
  • comment_notification_headers;

For our purposes, we’re going to use the first one namely comment_notification_text. As the name suggests, it allows us to modify the comment notification text to suit our needs. By default, it accepts only one argument called $notify_message if additional options are not specified in the add_filter function. If you want to add some static text to every outgoing e-mail notification that doesn’t change depending on the comment, then you don’t need anything else and the following function call will suffice:

add_filter('comment_notification_text', 'my_comment_notification_text');

If however, you want to do some processing on the comment URL for example, you need to also accept the comment ID as a parameter via:

add_filter('comment_notification_text', 'my_comment_notification_text', 10,2);

Where “10” refers to the default priority of the filter and “2” indicates that the custom function “’my_comment_notification_text” will accept $comment_id as another parameter. The two corresponding “’my_comment_notification_text” declarations are:

function my_comment_notification_text($notify_message)

and

function my_comment_notification_text($notify_message, $comment_id)

The custom function should return the string that will be sent as the comment notification content. Open up your functions.php file and add the following code. Keep in mind that this is most likely to be a theme independent change that you might want to preserve in case you upgrade or change a theme. In which case, it may be a good idea to abstract these changes to a custom plug-in that serves as a repository for theme independent code.

/* To include a legal message at the end of every comment notification */
add_filter('comment_notification_text', 'my_comment_notification_text');

function my_comment_notification_text($notify_message)
{
return $notify_message . ' Legal copyright/privacy/branding notice here';
}

As you can see, we simply append a little notification to the default $notify_message string that was being sent anyway. Save your functions.php or custom plug-in, write an anonymous comment on a test post and this is the notification you get:

copyright notice added

Before the April 15 update, you needed to use a regular expression to replace the default, link that internally used get_permalink() instead of get_comment_link(). That was a particular problem on posts that had a large number of comments with pagination enabled where a new page may have already overwritten the one displayed by default. Luckily, that’s no longer a problem.

But if you’re looking for yet more customization either to provide instructions to your post authors or to give them useful links for performing additional actions, modifying the comment notification text using the inbuilt filter is the most elegant way to go about it.

One Reply to “How to Change the Comment Notification Email in WordPress”!

  1. Avatar

    Hi, is there a way to include the Avatar picture of the person who commented ? Not only show text but text plus the picture of the person ?

Leave a Reply

This Website is Hosted by

Powered by RamNode

Disclosure: We receive a compensation from some of the companies whose products or services are presented on our website.