Close Comments across the network

Close Comments across the network

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.
[php]
<?php
/*
close comments globally
*/

function ra_switch_comments_off() {
global $wpdb;

$last_id = get_site_option( 'ra_comments_off', 1 );
if( $last_id < 0 )
return;

$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 );
return;
}
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' );
}
}
restore_current_blog();

update_site_option( 'ra_comments_off', $next_id );
}
add_action( 'init', 'ra_switch_comments_off' );
[/php]

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.

7 Comments
  • Konstantin
    Posted at 03:01h, 19 April Reply

    Would it make sense to schedule this to run only every 90 days?
    This way the script wouldn’t get called on every page load.
    Other than that: Nice, thank you! 🙂

    • Ron
      Posted at 19:37h, 21 April Reply

      Actually, it only runs once for each blog. After that it is essentially disabled. We installed it and removed it the next day.

  • WordPress: Open Comments More Selectively | Ipstenu on Tech
    Posted at 16:31h, 12 September Reply

    […] out Digging into WordPress – Easy Comment Management via SQL or the incredible Andrea’s How to Close Comments Across a MultiSite Network.Share this:Related Posts:Manually Customizing the WordPress Admin BarWordPress: EU […]

  • Useful Tidbits September 28, 2011 | Shelley Keith
    Posted at 00:48h, 29 September Reply

    […] Close Comments across the network – WordPress "Must-Use" Tutorials […]

  • Brian
    Posted at 17:23h, 02 October Reply

    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,

    • andrea
      Posted at 17:42h, 02 October Reply

      You changed the day value to 1 day and 1 day has not passed yet. 😉

      • Brian
        Posted at 17:46h, 02 October Reply

        OK. I thought it went by the date the post was made, not by the actual time the file is in the directory.

        Thanks again for all the help. I think I’m done now…until the next question comes along, that is.

Post A Comment