A PHP snippet for Daily Summary of New Comments for Post Authors.
function send_daily_comment_summary() {
global $wpdb;
// Get all authors with published posts.
$authors = get_users(array('who' => 'authors'));
foreach ($authors as $author) {
// Get new comments for the author's posts in the last 24 hours.
$comments = $wpdb->get_results($wpdb->prepare("
SELECT comment_author, comment_content, comment_post_ID
FROM $wpdb->comments
WHERE user_id != %d
AND comment_date >= DATE_SUB(NOW(), INTERVAL 1 DAY)
AND comment_post_ID IN (
SELECT ID FROM $wpdb->posts WHERE post_author = %d
)
AND comment_approved = 1
", $author->ID, $author->ID));
// If there are new comments, prepare the email.
if ($comments) {
$subject = "Daily Comment Summary for Your Posts";
$message = "Here are the new comments on your posts from the past 24 hours:\n\n";
foreach ($comments as $comment) {
$post_title = get_the_title($comment->comment_post_ID);
$comment_excerpt = wp_trim_words($comment->comment_content, 15);
$message .= "* {$comment->comment_author} commented on \"$post_title\": \"$comment_excerpt\"\n";
}
$headers = array('Content-Type: text/plain; charset=UTF-8');
wp_mail($author->user_email, $subject, $message, $headers);
}
}
}
// Schedule this to run daily.
if (!wp_next_scheduled('send_daily_comment_summary_hook')) {
wp_schedule_event(time(), 'daily', 'send_daily_comment_summary_hook');
}
add_action('send_daily_comment_summary_hook', 'send_daily_comment_summary');
function send_daily_comment_summary() {
global $wpdb;
// Get all authors with published posts.
$authors = get_users(array('who' => 'authors'));
foreach ($authors as $author) {
// Get new comments for the author's posts in the last 24 hours.
$comments = $wpdb->get_results($wpdb->prepare("
SELECT comment_author, comment_content, comment_post_ID
FROM $wpdb->comments
WHERE user_id != %d
AND comment_date >= DATE_SUB(NOW(), INTERVAL 1 DAY)
AND comment_post_ID IN (
SELECT ID FROM $wpdb->posts WHERE post_author = %d
)
AND comment_approved = 1
", $author->ID, $author->ID));
// If there are new comments, prepare the email.
if ($comments) {
$subject = "Daily Comment Summary for Your Posts";
$message = "Here are the new comments on your posts from the past 24 hours:\n\n";
foreach ($comments as $comment) {
$post_title = get_the_title($comment->comment_post_ID);
$comment_excerpt = wp_trim_words($comment->comment_content, 15);
$message .= "* {$comment->comment_author} commented on \"$post_title\": \"$comment_excerpt\"\n";
}
$headers = array('Content-Type: text/plain; charset=UTF-8');
wp_mail($author->user_email, $subject, $message, $headers);
}
}
}
// Schedule this to run daily.
if (!wp_next_scheduled('send_daily_comment_summary_hook')) {
wp_schedule_event(time(), 'daily', 'send_daily_comment_summary_hook');
}
add_action('send_daily_comment_summary_hook', 'send_daily_comment_summary');