free WordPress Theme

Before I get into the how, I think I may need to explain the why. πŸ˜‰

One of the tough job in running a large network of sites with multisite is controlling spam on the sub sites. Add that to users who come and go, abandoning their blogs with open settings, and very quickly your database(s) start filling up with spam and more spam. It’s like a spamfest in there.

I won’t get too deep in cleanup or management of spam comments in this post, but what I will cover is how to close comments on all old blogs in one swoop. This is the one thing that will stop the flood so you have time for cleanup.

Drop this code in the mu-plugins folder and it will close the comments on all blogs that haven’t been posted to in over 90 days, on all posts over 90 days old.
close comments globally

function ra_switch_comments_off() {
global $wpdb;

$last_id = get_site_option( ‘ra_comments_off’, 1 );
if( $last_id < 0 )

$next_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE blog_id > %d LIMIT 1", $last_id ) );
if( !$next_id ) {
update_site_option( ‘ra_comments_off’, -1 );
switch_to_blog( $next_id );
if( ‘1’ != get_option( ‘close_comments_for_old_posts’ ) ) {
$post = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_status = ‘publish’ AND DATE_ADD(post_date, INTERVAL 8 MONTH) > NOW() LIMIT 1" );
if( empty( $post ) ) {
update_option( ‘close_comments_for_old_posts’, ‘1’ );
update_option( ‘close_comments_days_old’, ’90’ );

update_site_option( ‘ra_comments_off’, $next_id );
add_action( ‘init’, ‘ra_switch_comments_off’ );

You can leave it there to keep on rocking, or you can remove it. You can even edit the value to change it from 90 days to somethign else.

WARNING: this *WILL* override each site’s individual settings, mostly because we’re tossed it in mu-plugins and we’re accessing each site individually to update the database. Use this only if you really need it.


  1. OK, update. I got the code working, and put it into my mu-plugins folder and it is showing up in my dashboard. I also changed the day value (to close comments) to 1, rather than 90 until I can get some other things sorted out.

    But, when I went and checked some other sites in the network (both logged in and out) the comments are still being accepted, even on old posts.

    Thanks for the help,