Since I installed this plug-in, instead of the usual HUNDREDS of spam comments/trackbacks, I’ve only had SIX. Most spam is on old articles; this plug-in automatically shuts off comments for articles at least 14 days old. Just copy the code below into notepad and save it as autoshutoff.php, upload it to your /wp-content/plugins/ directory, and activate the plug-in from your dashboard! Here’s the code:
/*
Plugin Name: Auto-Close Comments
Version: 0.2
Plugin URI: http://codex.wordpress.org/Plugins/Auto_shutoff_comments
Description: Autoclose comments after 21 days.
Author: Scott Hanson
Author URI: http://www.papascott.de/
*/
/* Add an index on comment_status to wp_posts to speed this up. */
function autoclose_comments() {
global $wpdb, $tableposts;
if (!isset($tableposts))
$tableposts = $wpdb->posts;
// Set $age to the age at which a post should become stale
$age = ‘14 DAY’;
$date = $wpdb->get_var(”
SELECT DATE_ADD(DATE_SUB(CURDATE(), INTERVAL $age), INTERVAL 1 DAY)
“);
$wpdb->query(”
UPDATE $tableposts
SET comment_status = ‘closed’, ping_status = ‘closed’
WHERE (comment_status = ‘open’ OR ping_status = ‘open’)
AND post_status = ‘publish’
AND post_date < '$date'
");
}
add_action('publish_post', 'autoclose_comments', 7);
add_action('edit_post', 'autoclose_comments', 7);
add_action('delete_post', 'autoclose_comments', 7);
add_action('comment_post', 'autoclose_comments', 7);
add_action('trackback_post', 'autoclose_comments', 7);
add_action('pingback_post', 'autoclose_comments', 7);
add_action('edit_comment', 'autoclose_comments', 7);
add_action('delete_comment', 'autoclose_comments', 7);
add_action('template_save', 'autoclose_comments', 7);
?>