最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Disabling Comment Notifications for Post Author

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Jun 19, 2014 at 17:22 birgire 68.1k7 gold badges120 silver badges252 bronze badges asked Jun 19, 2014 at 15:18 Tim McClureTim McClure 3121 gold badge3 silver badges11 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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/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/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.

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(
        '[email protected]',
        '[email protected]',
    );
    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 );
发布评论

评论列表(0)

  1. 暂无评论