Using WordPress ‘notify_post_author’ PHP filter

The notify_post_author WordPress PHP filter allows you to control whether or not to send the post author new comment notification emails, overriding the site setting.

Usage

add_filter( 'notify_post_author', 'your_custom_function', 10, 2 );

function your_custom_function( $maybe_notify, $comment_id ) {
    // your custom code here
    return $maybe_notify;
}

Parameters

  • $maybe_notify (bool): Whether to notify the post author about the new comment.
  • $comment_id (int): The ID of the comment for the notification.

More information

See WordPress Developer Resources: notify_post_author

Examples

Disable comment notifications for all posts

Disable comment notifications for all authors, regardless of site settings.

function disable_comment_notifications( $maybe_notify, $comment_id ) {
    return false;
}
add_filter( 'notify_post_author', 'disable_comment_notifications', 10, 2 );

Enable comment notifications for specific author

Enable comment notifications for a specific author, overriding the site settings.

function enable_notifications_for_author( $maybe_notify, $comment_id ) {
    $comment = get_comment( $comment_id );
    $post = get_post( $comment->comment_post_ID );
    if ( $post->post_author == 5 ) { // Change 5 to the desired author ID
        return true;
    }
    return $maybe_notify;
}
add_filter( 'notify_post_author', 'enable_notifications_for_author', 10, 2 );

Disable notifications for comments on specific post type

Disable comment notifications for a specific custom post type, overriding the site settings.

function disable_notifications_for_post_type( $maybe_notify, $comment_id ) {
    $comment = get_comment( $comment_id );
    $post = get_post( $comment->comment_post_ID );
    if ( $post->post_type == 'custom_post_type' ) {
        return false;
    }
    return $maybe_notify;
}
add_filter( 'notify_post_author', 'disable_notifications_for_post_type', 10, 2 );

Enable notifications for comments with specific keyword

Enable comment notifications if the comment contains a specific keyword, overriding the site settings.

function enable_notifications_for_keyword( $maybe_notify, $comment_id ) {
    $comment = get_comment( $comment_id );
    if ( strpos( $comment->comment_content, 'important' ) !== false ) {
        return true;
    }
    return $maybe_notify;
}
add_filter( 'notify_post_author', 'enable_notifications_for_keyword', 10, 2 );

Disable notifications for comments by logged-out users

Disable comment notifications for comments made by logged-out users, overriding the site settings.

function disable_notifications_for_guests( $maybe_notify, $comment_id ) {
    $comment = get_comment( $comment_id );
    if ( $comment->user_id == 0 ) {
        return false;
    }
    return $maybe_notify;
}
add_filter( 'notify_post_author', 'disable_notifications_for_guests', 10, 2 );