I am trying to disable comment notifications for post authors (or anyone else besides the site’s admin) for a client’s site. I’ve attempted to create a plugin that uses the pluggable function wp_notify_postauthor, but it does not seem to have an effect.

Here’s the plugin code:

<?php
/**
 *
 *
 * @package   Disable_plugin_notifications
 * @author    Me
 * @link      <hidden>

 *
 * @wordpress-plugin
 * Plugin Name:       Disable Comment Notifications
 * Plugin URI:        <hidden>
 * Description:       <hidden>
 * Version:           1.0.0
 * Author:            <hidden>
 * Author URI:        <hidden>
 */


// Disabling comment notifications for post authors
if ( !function_exists( 'wp_notify_postauthor' ) ) {
    function wp_notify_postauthor() {
        return;
    }
}

I’ve also tried it without the ‘return’ in the function.

2 Answers
2

I skimmed through the source of the wp_notify_postauthor() function and noticed the comment_notification_recipients filter.

I wonder if you could simplify your plugin to the following code snippet:

<?php
 /** 
  * Plugin Name: Disable comment/trackback/pingback notifications emails 
  * Plugin URI:  http://wordpress.stackexchange.com/a/150141/26350
  */

add_filter( 'comment_notification_recipients', '__return_empty_array', PHP_INT_MAX );
add_filter( 'comment_moderation_recipients',   '__return_empty_array', PHP_INT_MAX );

where we use an empty $emails array to prevent any notification emails from being sent.

The first filter is to stop wp_notify_postauthor() and the second to stop wp_notify_moderator().

If you want only the admin user to receive email notifications, you can use this version:

<?php
 /** 
  * Plugin Name: Disable comment/trackback/pingback notifications emails except for admins.
  * Plugin URI:  http://wordpress.stackexchange.com/a/150141/26350
  */

add_filter( 'comment_notification_recipients', '__return_empty_array', PHP_INT_MAX );
add_filter( 'comment_moderation_recipients', 
    function( $emails )
    {
        // only send notification to the admin:
        return array( get_option( 'admin_email' ) );
    }
, PHP_INT_MAX );

We could also override these two pluggable functions, but I don’t use that here.

Leave a Reply

Your email address will not be published. Required fields are marked *