The comment_notification_notify_author WordPress PHP filter allows you to control whether comment authors are notified of their comments on their own posts.
Usage
add_filter('comment_notification_notify_author', 'your_custom_function', 10, 2);
function your_custom_function($notify, $comment_id) {
// Your custom code here
return $notify;
}
Parameters
$notify(bool) – Whether to notify the post author of their own comment. Default isfalse.$comment_id(string) – The comment ID as a numeric string.
More information
See WordPress Developer Resources: comment_notification_notify_author
Examples
Notify authors of their comments on their own posts
To notify comment authors of their comments on their own posts, return true:
add_filter('comment_notification_notify_author', 'notify_authors_own_comments', 10, 2);
function notify_authors_own_comments($notify, $comment_id) {
return true;
}
Notify authors only if the comment is a reply
Notify comment authors only if the comment is a reply to another comment:
add_filter('comment_notification_notify_author', 'notify_authors_on_replies', 10, 2);
function notify_authors_on_replies($notify, $comment_id) {
$comment = get_comment($comment_id);
if ($comment->comment_parent > 0) {
return true;
}
return false;
}
Notify authors only if their comment is approved
Notify comment authors only if their comment is approved:
add_filter('comment_notification_notify_author', 'notify_authors_approved_comments', 10, 2);
function notify_authors_approved_comments($notify, $comment_id) {
$comment = get_comment($comment_id);
if ($comment->comment_approved == '1') {
return true;
}
return false;
}
Notify authors based on custom user meta
Notify comment authors only if they have a specific custom user meta value:
add_filter('comment_notification_notify_author', 'notify_authors_based_on_meta', 10, 2);
function notify_authors_based_on_meta($notify, $comment_id) {
$comment = get_comment($comment_id);
$user_id = $comment->user_id;
$meta_value = get_user_meta($user_id, 'your_custom_meta_key', true);
if ($meta_value == 'your_desired_value') {
return true;
}
return false;
}
Notify authors only for specific post types
Notify comment authors only if their comment is on a specific post type:
add_filter('comment_notification_notify_author', 'notify_authors_specific_post_types', 10, 2);
function notify_authors_specific_post_types($notify, $comment_id) {
$comment = get_comment($comment_id);
$post_id = $comment->comment_post_ID;
$post_type = get_post_type($post_id);
if ($post_type == 'your_specific_post_type') {
return true;
}
return false;
}