WordPress: Moderate comments using regular expressions

7
2781

While WordPress allows us to moderate and blacklist comments using two nifty text-areas within Settings->Discussion called “Comment Moderation” and “Comment Blacklist” you may need to add some extra functionality so that RegEx or Regular Expressions can also be used to directly approve, moderate, send to spam or blacklist comments.

Fortunately there is the “pre_comment_approved” filter which we can use for this purpose.

Lets setup our scenario, I want to add some moderation so that users don’t add URL’s in comments and I will make a quick check for domain names in the comment content.

Note: The code below would either go in your themes functions.php or a custom plugin.

In order to keep the example simple I am going to use a generic domain matching regex.

Example 1. Lets say we want to moderate the comments.

Here we set $approved to the integer 0(zero)

function te_check_comment($approved, $commentdata){
	if(preg_match('#\b((?=[a-z0-9-]{1,63}\.)[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b#i',$commentdata["comment_content"])){
		$approved = 0;
	}
	return $approved;
}
add_action('pre_comment_approved', 'te_check_comment', 10, 2);

Example 2. Here we want to mark the comments as spam.

$approved is set to the string ‘spam’

function te_check_comment($approved, $commentdata){
	if(preg_match('#\b((?=[a-z0-9-]{1,63}\.)[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b#i',$commentdata["comment_content"])){
		$approved = 'spam';
	}
	return $approved;
}
add_action('pre_comment_approved', 'te_check_comment', 10, 2);

Example 3. Here we want to send the comments directly to trash.

$approved is set to the string ‘trash’

function te_check_comment($approved, $commentdata){
	if(preg_match('#\b((?=[a-z0-9-]{1,63}\.)[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b#i',$commentdata["comment_content"])){
		$approved = 'trash';
	}
	return $approved;
}
add_action('pre_comment_approved', 'te_check_comment', 10, 2);

Example 4. We want to bypass everything and Approve the comment if it matches our string.

$approved to the integer 1(one)

function te_check_comment($approved, $commentdata){
	if(preg_match('#\b((?=[a-z0-9-]{1,63}\.)[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b#i',$commentdata["comment_content"])){
		$approved = 1;
	}
	return $approved;
}
add_action('pre_comment_approved', 'te_check_comment', 10, 2);

Keep in mind that all the comments submitted will be saved in the WordPress comments table with the appropriate approved status.

7 COMMENTS

  1. Hi,

    I am trying to add this to the comment.php file under wp-includes in my wordpress without success.
    I have on my wp also a theme and Buddypress installaed
    I want to put spam for URL or othe regex in the comments but I have no success with the code in this article

    Could you help me ?
    Thanks!

    • Hi Walter, thank you for your comment. The code would either go in your themes functions.php or a custom plugin. I will add a note to the article about this.

  2. Hi TechEarl,
    I have add the function :

    function te_check_comment($approved, $commentdata){
    if(preg_match(‘#\b((?=[a-z0-9-]{1,63}\.)[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b#’,$commentdata[“comment_content”])){
    $approved = ‘spam’;
    }
    return $approved;
    }
    add_action(‘pre_comment_approved’, ‘te_check_comment’, 10, 2);

    to the functions.php on my theme but I see no SPAM message appear on a comment while it is an URL
    I really don’t understand how to use this function and it is really important for my project.

    Could you let me know some tip ?

    Thanks a lot

    Walter

    • There should be some particular setting in WP to trigger the SPAM message when it recognize the URL ?

      Thanks

      Walter

      • Hi Walter, I tested the spam item and it works fine. The comment will not be published and go into the Span section in the admin. I am providing a simple example and there is no message generated, you can extend to do that if you want. I also made a slight change to the regex pattern to match both lower and uppercase characters, but again that part is all up to you.

        • Thanks a lot!!! I am waiting for your example so that I can test it.
          Please could you provide soon … thanksssss a loootttt 🙂

        • Hi TechEarl,

          you are completely right, I have add your spam function to the functions.php in my theme (without Buddypress) and it works well.
          The message goes in the spam.
          My problem comes when I use buddypress and another theme I have installed.
          I have to understand why…

          Thanks really a lot

          Walter

LEAVE A REPLY

Please enter your comment!
Please enter your name here