How to filter names on WordPress comments?

I would like to filter names on my comments section.

For example, if a user type a name listed on my black-list, the comment will not be saved or will be marked as spam. Any idea on where to start or if it’s even possible?

Related posts

Leave a Reply

2 comments

  1. I don’t know if this is what you’re looking for, but WordPress already has a plugin for Spam protection called Askimet Just check if you have it installed in your system.

  2. Yes Askimet is a good option, but if some one want to do it manually ( for some reason ) you can use wp_insert_comment for this purpose.

    do something like this.

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