4

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.

5

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.

  • Tried this, but still got the notification email. – Tim McClure Jun 19 '14 at 17:53
  • ok, I can't check it out now, will try it out later. But you can also try to set the comments_notify option to 0. – birgire Jun 19 '14 at 18:30
  • I updated the answer, I did a silly mistake in the first version ;-) I've tested it and it seems to stop the comment notification emails on my install. Did you try this on a vanilla install? This method will of course not work if someone overrides the function and removes this filter. – birgire Jun 19 '14 at 19:25
  • Hmm, still happening for me, I'll try it out on a vanilla install tonight and see if it works there. – Tim McClure Jun 19 '14 at 19:33
  • 1
    That change did the trick! – Tim McClure Jun 23 '14 at 13:07
0

Following up on @birgire's nice answer, here are some other variations:

1) Eliminate specific emails from the sending list

<?php
 /**
  * Plugin Name: Disable comment/trackback/pingback notifications for specific users.
  */
function squarecandy_eliminate_admin_comment_emails( $emails ) {
    // do not send to these specific emails
    $donotsend = array(
        'userone@example.com',
        'usertwo@example.com',
    );
    foreach ( $emails as $key => $email ) {
        if ( in_array( $email, $donotsend ) ) {
            unset( $emails[$key] );
        }
    }
    return $emails;
}
add_filter( 'comment_notification_recipients', 'squarecandy_eliminate_admin_comment_emails', PHP_INT_MAX );
add_filter( 'comment_moderation_recipients', 'squarecandy_eliminate_admin_comment_emails', PHP_INT_MAX );

2) Stop sending comment notifications to all users within certain roles

<?php
 /**
  * Plugin Name: Disable comment/trackback/pingback notifications emails except for certain roles.
  */
function squarecandy_eliminate_admin_comment_emails( $emails ) {
    // get all the users of the roles you want to restrict
    $users = get_users( 
        array(
            'role__in' => array(
                // this is the list of roles to repress comment notifications for
                'Editor',
                'Author',
            ),
            'fields' => array(
                'user_email',
            ),
        ),
    );
    // make an array of the emails not to send to
    $donotsend = array();
    foreach ( $users as $user ) {
        $donotsend[] = $user->user_email;
    }

    foreach ( $emails as $key => $email ) {
        if ( in_array( $email, $donotsend ) ) {
            unset( $emails[$key] );
        }
    }
    return $emails;
}
add_filter( 'comment_notification_recipients', 'squarecandy_eliminate_admin_comment_emails', PHP_INT_MAX );
add_filter( 'comment_moderation_recipients', 'squarecandy_eliminate_admin_comment_emails', PHP_INT_MAX );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.