Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariusvetrici/44acdacd6b283ccd46d9 to your computer and use it in GitHub Desktop.
Save mariusvetrici/44acdacd6b283ccd46d9 to your computer and use it in GitHub Desktop.
WordPress - Replace comment notification email with Mandrill template
<?php
define( 'ABC_NEW_COMMENT_EMAIL_MARKER', '|||abcNewComment||' );
add_filter( "comment_notification_text", "abc_comment_notification_text", 10, 2 );
/**
* Filter for changing the content of the email notification sent upon new comment.
*
* This function replaces the original comment email with a prefix + the comment_id.
* Later it will be extracted and inserted into Mandrill template as variables by abc_replace_mandrill_template.
*
* See the other function abc_replace_mandrill_template for full reference.
*
* @param $notify_message the original email message
* @param $comment_id the comment id
*
* @return string the new message structured as |||abcNewComment||comment_id
*/
function abc_comment_notification_text( $message_template, $comment_id ) {
$message_template = ABC_NEW_COMMENT_EMAIL_MARKER . $comment_id;
return $message_template;
}
add_filter( 'mandrill_payload', 'abc_replace_mandrill_template' );
/**
* Replace default Mandrill email template with the corresponding one
*
* @param $message Mandrill message object as defined in https://mandrillapp.com/api/docs/messages.html#method=send-template
*
* @return object the filtered $message object
*/
function abc_replace_mandrill_template( $message ) {
// convenience variable
$autoTags = $message['tags']['automatic'];
// If this is a new comment notification email (to author) and
// the email body has been prepared by abc_comment_notification_text function by
// inserting "|||abcNewComment||comment_id" into the message body, then we
// - replace the template with the template for comment emails
// - insert the corresponding variables into the template
if ( in_array( 'wp_wp_notify_postauthor', $autoTags )
&& false !== strpos( $message['template']['content'][0]['content'], ABC_NEW_COMMENT_EMAIL_MARKER )
) {
// Get the comment id from email
$comment_id = (int) substr( $message['template']['content'][0]['content'], strlen( ABC_NEW_COMMENT_EMAIL_MARKER ) );
// We've got a valid $comment_id
if ( $comment_id > 0 ) {
// Extract the needed variables for template
$comment = get_comment( $comment_id );
if ( empty( $comment ) ) {
$comment_content = "";
} else {
$comment_content = nl2br($comment->comment_content);
}
$post = get_post( $comment->comment_post_ID );
$author = get_userdata( $post->post_author );
$author_name = $author->first_name . " " . $author->last_name;
if ( ! trim( $author_name ) ) {
$author_name = $author->user_login;
}
$slug = $post->post_name;
// Insert the vars into the template
$message['template']['content'][] =
array('name' => 'author_name',
'content' => "Hello " . $author_name . ",<br><br>");
$message['template']['content'][] =
array('name' => 'comment_content',
'content' => $comment_content);
$message['template']['content'][] =
array('name' => 'task_link',
'content' => '<a href="' . site_url() . "/task/{$slug}/#comments" .
'" style="color: black;text-decoration:none;padding:5px 60px;margin:10px;font-size:28px;line-height: 28px;">Reply Here</a>');
// Change the template
$message['template']['name'] = 'New Comment Email';
}
}
return $message;
}
@mariusvetrici
Copy link
Author

See this Gist for Mandrill template:
https://gist.github.com/mariusvetrici/986fa6d350d4b6ebfcaa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment