Auto Shutoff Comments And Trackbacks

You are searching for a Plugin, which deactivates in a specific time interval the trackbacks/pingbacks function?

Is there a WordPress Plugin, which explicitly suppress trackbacks and comments on articles older than X days and no moderation is necessary?

I show you a fast and easy solution, I just extended an already existing solution. I used the Plugin Auto-Close Comments, because I just have to change one field in the SQL-request. The Plugin is build now, that comments and trackbacks are deactivated after a certain time.

You can also enter this function in functions.php of your active theme, however you like it.

This function will be builtin WordPress 2.7, another reason to upgrade to the newest and greatest WordPress version. Thanks DD32 for the note!

<?php
/*
Plugin Name: Auto-Close Comments/ Trackbacks
Version: 0.1
*/

// close comments in database
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 = '21 DAY';

    $date = $wpdb->get_var("
        SELECT DATE_ADD(DATE_SUB(CURDATE(), INTERVAL $age), INTERVAL 1 DAY)
    ");

    $wpdb->query("
        UPDATE $tableposts
        SET comment_status = 'closed'
        WHERE comment_status = 'open'
        AND post_status = 'publish'
        AND post_date < '$date'
    ");
}

// close trackback in database
function autoclose_trackback() {
    global $wpdb, $tableposts;

    if (!isset($tableposts))
        $tableposts = $wpdb->posts;

    // Set $age to the age at which a post should become stale
    $age = '21 DAY';

    $date = $wpdb->get_var("
        SELECT DATE_ADD(DATE_SUB(CURDATE(), INTERVAL $age), INTERVAL 1 DAY)
    ");

    $wpdb->query("
        UPDATE $tableposts
        SET ping_status = 'closed'
        WHERE comment_status = 'open'
        AND post_status = 'publish'
        AND post_date < '$date'
    ");
}

add_action('publish_post',   'autoclose_trackback', 7);
add_action('edit_post',      'autoclose_trackback', 7);
add_action('delete_post',    'autoclose_trackback', 7);
add_action('comment_post',   'autoclose_trackback', 7);
add_action('trackback_post', 'autoclose_trackback', 7);
add_action('pingback_post',  'autoclose_trackback', 7);
add_action('edit_comment',   'autoclose_trackback', 7);
add_action('delete_comment', 'autoclose_trackback', 7);
add_action('template_save',  'autoclose_trackback', 7);

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);
?>

Download:

Download as ZIP file: timeout.zip - 1 KByte

Comments are closed.

9 comments

  1. Ajay

    Hi, about a while back I released a plugin Auto-Close Comments, Pingbacks and Trackbacks that fulfils this purpose.

    Do check it out and let me know what can be improved.

  2. DD32

    I'm pleased to note, that WordPress 2.7 has this function builtin, Under the Discussion settings you'll find: Automatically close comments on articles older than xx days

    It defaults to 14 days, and Doesnt use a a couple of queries to achieve it, It mearly "closes on the fly".

  3. Alex

    Thank you Ajay for your link to your plugin but as you can see, DD32 said, this function will be builtin WordPress 2.7.

    Thanks DD32 for your information, we updated the post accordingly.

  4. John P

    Ajay's plugin differentiates between pages and posts which the core does not do. Ajay's plugin also allows you to keep specific posts open past the stipulated aging.

    The downside of Ajay's plugin is that it updates the posts table via a wp-cron query. Ajay does provide a couple of 'undo' queries which are handy.

    I believe the core doesn't send update queries to the posts table but rather filters the comments_status field to 'open' or 'closed' based on post_date. Personally, I prefer this approach rather than sending regularly scheduled update queries to the posts table.

  5. Alex

    Thanks John, for the information about the advantages of Ajay's Plugin compare the the core function in WordPress.

  6. Ajay

    Thanks for the comparison John.

    I chose to go with the cron option because that takes place on the db only once a day as opposed to a contant check. (I hope that is how WP functions)

    IMO, I prefer not running the cron but doing an occasional manual close.

  7. John P

    It took me all of a minute to code a function in my theme which will allow comments on pages to stay open. Ryan built the feature into the WP 2.7 core using a filter so all I had to do was tweak and rename his core function, remove the core filter and add my new filter. Adding exception post IDs could be done with a foreach loop with just a little work.

  8. Smart Car Care

    Hello,
    I am Angela Rickson, I always read this sites something everyday because I like its component and I get many advice after read its article. I tell to my other friends about this site. I hope you will read my this comment and you will remember me. i want you always make new articles like this.The downside of Ajay's plugin is that it updates the posts table via a wp-cron query. Ajay does provide a couple of 'undo' queries which are handy.
    Angelaric01

One pingback

  1. WordPress links - Week 50 2008 | WPStart.org - WordPress themes, plugins and news