25% off ProSNIPC25OFF

Close Comments on Old Posts

Programmatically closes comments and pings on posts older than a configurable number of days.

PHPby SnipCraft
php
<?php
add_filter( 'comments_open', 'scseed_close_old_post_comments', 10, 2 );
add_filter( 'pings_open',    'scseed_close_old_post_comments', 10, 2 );
if ( ! function_exists( 'scseed_close_old_post_comments' ) ) {
    function scseed_close_old_post_comments( $open, $post_id ) {
        if ( ! $open ) {
            return false;
        }
        // Number of days after publication before comments are closed.
        $close_after_days = 180;
        $post_date = get_post_field( 'post_date', $post_id );
        $days_old  = ( time() - strtotime( $post_date ) ) / DAY_IN_SECONDS;
        return $days_old <= $close_after_days;
    }
}
#comments#content#moderation#spam