如何过滤Wordpress评论上的名字



我想在我的评论部分过滤名字。

例如,如果用户键入我的黑名单上列出的姓名,则该评论将不会保存或被标记为垃圾邮件。关于从哪里开始或是否可能的任何想法?

我不知道这是否是你要找的,但Wordpress已经有一个名为Askimet的垃圾邮件保护插件 只需检查您的系统中是否安装了它。

是的,Askimet是一个不错的选择,但如果有人想手动完成(出于某种原因),您可以使用wp_insert_comment来实现此目的。

做这样的事情。

function check_comment_inserted( $comment_id, $comment_object ) {
    /**
     *  Here is the list of properties, that comment_object carrying 
     *      
     *  $comment_object->comment_ID": "29",
     *  $comment_object->comment_post_ID": "1",
     *  $comment_object->comment_author": "Sark",
     *  $comment_object->comment_author_email": "mycholan@ymail.com",
     *  $comment_object->comment_author_url": "http:\/\/sarkware.com",
     *  $comment_object->comment_author_IP": "::1",
     *  $comment_object->comment_date": "2016-05-19 20:27:53",
     *  $comment_object->comment_date_gmt": "2016-05-19 20:27:53",
     *  $comment_object->comment_content": "Fifth comment",
     *  $comment_object->comment_karma": "0",
     *  $comment_object->comment_approved": "0",
     *  $comment_object->comment_agent": "Mozilla\/5.0 (X11; Linux x86_64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/50.0.2661.87 Safari\/537.36 OPR\/37.0.2178.32",
     *  $comment_object->comment_type": "",
     *  $comment_object->comment_parent": "0",
     *  $comment_object->user_id": "0"
    **/
    $block_list = array( 'name1', 'name2', 'name3', 'name4' );
    foreach( $block_list as $name ) {
        if ( stripos( $comment_object->comment_content, $name ) !== false ) {
            // now set this comment status
            // Valid comment status : 'hold', 'approve', 'spam', or 'trash'
            wp_set_comment_status( $comment_id, "spam" );
            break;
        }
    }   
}
add_action( 'wp_insert_comment', 'check_comment_inserted', 99, 2 );

最新更新