Filtering Comments Via IP in WordPress

One feature that is missing in WordPress is a decent way to filter out comments by IP Address. I decided to tackle that today. First I will do it via a quick hack and then expand it to more tightly integrate with the backend. If someone would like to figure out how to make it a plugin, that would be awesome.

  1. First you need to add a table to the WordPress database. I named mine wp_banned and added a simple column: IpAddress.
  2. Open up /wp-includes/comment.php and look around line 676 for the wp_new_comment function. Add the following to the beginning:
    global $wpdb;
    
    $testIp = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] );
    
    $ipResults = $wpdb->get_results("SELECT * FROM wp_banned WHERE IpAddress = '".$testIp."'");
    if(count($ipResults) > 0)
    	return -1;
  3. Upload, add ips to the database and then watch them not appear anymore.

Now I just need to figure out how to add some nice buttons to the comment page to “Ban and Spam” comments…

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>