禁用帖子作者的评论通知

时间:2014-06-19 作者:Tim McClure

我正在尝试为客户端网站禁用帖子作者(或网站管理员以外的任何其他人)的评论通知。我试图创建一个使用可插入功能的插件wp_notify_postauthor, 但它似乎没有效果。

以下是插件代码:

<?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;
    }
}
我也尝试过在函数中没有“return”的情况下使用它。

2 个回复
最合适的回答,由SO网友:birgire 整理而成

我浏览了一下wp_notify_postauthor() 功能并注意到comment_notification_recipients 滤器

我想知道您是否可以将插件简化为以下代码段:

<?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 );
我们使用空的$emails 阵列以防止发送任何通知电子邮件。

第一个过滤器将停止wp_notify_postauthor() 第二个停下来wp_notify_moderator().

如果只希望管理员用户接收电子邮件通知,可以使用此版本:

<?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 );
我们也可以重写这两个可插入函数,但我这里不使用它。

SO网友:squarecandy

继@birgire的漂亮回答之后,以下是一些其他变体:

1) 从发送列表中删除特定电子邮件

<?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)停止向特定角色内的所有用户发送评论通知

<?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 );

结束

相关推荐

WP_LOCALIZE_SCRIPT与WordPress中的mce_外部_plugins

我不确定这是否可能。我可以使用wp_localize_script 具有mce_external_plugins 滤器我想向tinymce插件脚本发送一个变量。例如:add_filter( \"mce_external_plugins\", array( &$this, \'add_test_plugin\' ) ); public function add_test_plugin( $plugin_array ){ global $pagenow;&#