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.
- First you need to add a table to the WordPress database. I named mine wp_banned and added a simple column: IpAddress.
- Open up /wp-includes/comment.php and look around line 676 for the wp_new_comment function. Add the following to the beginning:
1 2 3 4 5 6 7
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;
- 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…
