将评论标记为垃圾评论时返回错误

时间:2013-05-09 作者:LouieV

Hello Im使用一个自定义过滤器pre_comment_approved 以习惯的方式寻找坏话。现在,当注释被标记时,它会返回spam.

我如何将错误发回评论页面,以便用户知道他/她的电子邮件由于使用了列入黑名单的单词而被发送到垃圾邮件?

谢谢

function my_wp_blacklist_check( $approved , $commentdata ) {

$mod_keys = trim( get_option(\'blacklist_keys\') );
if ( \'\' == $mod_keys )
    return false; // If moderation keys are empty
$words = explode("\\n", $mod_keys );

foreach ( (array) $words as $word ) {
    $word = trim($word);
    str_pad($word, 1, STR_PAD_BOTH);
    // Skip empty lines
    if ( empty($word) ) { continue; }

    // Do some escaping magic so that \'#\' chars in the
    // spam words don\'t break things:
    $word = preg_quote($word, \'#\');

    $pattern = "#\\b$word\\b#i";
    if (preg_match($pattern, $commentdata[\'comment_content\'])) {
        return \'spam\';
    }
}
return true;
}
add_filter( \'pre_comment_approved\' , \'my_wp_blacklist_check\' , \'99\', 2 );

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

这是我以前处理将消息传递回用户的方式:在当前函数中,设置transient 随信附上:

set_transient( \'admin_notice\', \'Please put down your weapon. You have 20 seconds to comply.\' );
然后添加一个新的挂钩函数:

function admin_notices() {
    $notice = get_transient( \'admin_notice\' );
    if ( $notice ) {
        echo \'<div class="error"><p>\' . $notice . \'</p></div>\';
        delete_transient( \'admin_notice\' );
    }
}

add_action( \'admin_notices\', \'admin_notices\' );

结束