Created
March 25, 2016 03:28
-
-
Save QROkes/428e292f687ba3509cd4 to your computer and use it in GitHub Desktop.
Sends an email notification when a comment receives a reply - WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Based on: https://wordpress.org/plugins/comment-reply-email-notification/ */ | |
add_action('wp_insert_comment', 'qr_comment_notification', 99, 2); | |
add_action('wp_set_comment_status','qr_comment_status_update', 99, 2); | |
add_filter('wp_mail_content_type', function($contentType) { return 'text/html'; }); | |
/** | |
* Sends an email notification when a comment receives a reply | |
* @param int $commentId The comment ID | |
* @param object $comment The comment object | |
* @return boolean | |
*/ | |
function qr_comment_notification($commentId, $comment) { | |
if ($comment->comment_approved == 1 && $comment->comment_parent > 0) { | |
$parent = get_comment($comment->comment_parent); | |
$body = __('Hi ', 'cren-plugin') . $parent->comment_author . ','; | |
$body .= '<br><br>' . $comment->comment_author . __(' has replied to your comment on ', 'cren-plugin'); | |
$body .= '<a href="' . get_permalink($parent->comment_post_ID) . '">' . get_the_title($parent->comment_post_ID) . '</a>'; | |
$body .= '<br><br><em>"' . esc_html($comment->comment_content) . '"</em>'; | |
$body .= '<br><br><a href="' . get_comment_link($parent->comment_ID) . '">' . __('Click here to reply', 'cren-plugin') . '</a>'; | |
$email = $parent->comment_author_email; | |
$title = '[QROkes] ' . __('New reply to your comment', 'cren-plugin', $body); | |
$headers = 'From: MyName <[email protected]>'; | |
wp_mail($email, $title, $body, $headers); | |
} | |
} | |
/** | |
* Sends a notification if a comment is approved | |
* @param int $commentId The comment ID | |
* @param string $commentStatus The new comment status | |
* @return boolean | |
*/ | |
function qr_comment_status_update($commentId, $commentStatus) { | |
$comment = get_comment($commentId); | |
if ($commentStatus == 'approve') { | |
cren_comment_notification($comment->comment_id, $comment); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment