How to Set Auto Shutoff for Comments and Trackbacks in WordPress

Home » Snippets » How to Set Auto Shutoff for Comments and Trackbacks in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you tired of receiving spam comments and trackbacks on your WordPress site? Do you want to automatically shut off comments or trackbacks for older posts? Fortunately, there’s an easy way to do it with just a few lines of code. In this article, we’ll show you how to set up automatic comment and trackback shutoffs for your WordPress website.

					function WPTurbo_auto_close_comments( $post_id ) {
    // Define the number of days to close comments and trackbacks
    $days_to_close = 30;
    
    // Get the post publish date and current date
    $post_date = strtotime( get_the_date( 'Y-m-d', $post_id ) );
    $current_date = strtotime( current_time( 'mysql' ) );
    
    // Calculate the number of seconds in the defined number of days
    $seconds_to_close = $days_to_close * 24 * 60 * 60;
    
    // If the post is older than the defined number of days...
    if ( ( $current_date - $post_date ) >= $seconds_to_close ) {
        // Close comments and trackbacks
        update_post_meta( $post_id, '_ping_status', 'closed' );
        update_post_meta( $post_id, '_comments_status', 'closed' );
    }
}
add_action( 'publish_post', 'WPTurbo_auto_close_comments' );
				

This code snippet allows you to automatically close the comments and trackbacks on your WordPress posts after a certain number of days. This can be useful if you want to prevent spam or outdated comments on your site.

First, we define the number of days after which we want to close the comments and trackbacks by setting the $days_to_close variable to 30:

$days_to_close = 30;

Next, we get the publish date of the post and the current date:

$post_date = strtotime( get_the_date( 'Y-m-d', $post_id ) );
$current_date = strtotime( current_time( 'mysql' ) );

We use the strtotime and get_the_date functions to convert the publish date of the post to a Unix timestamp, which represents the number of seconds since January 1, 1970, and get the current date as a Unix timestamp using the current_time function.

After that, we calculate the number of seconds in the defined number of days by multiplying the number of days by 24 (hours per day) by 60 (minutes per hour) by 60 (seconds per minute):

$seconds_to_close = $days_to_close * 24 * 60 * 60;

Now we check if the post is older than the defined number of days. If it is, we automatically close the comments and trackbacks using the update_post_meta function:

if ( ( $current_date - $post_date ) >= $seconds_to_close ) {
    update_post_meta( $post_id, '_ping_status', 'closed' );
    update_post_meta( $post_id, '_comments_status', 'closed' );
}

The update_post_meta function allows us to update the meta data of the post with the ID $post_id. We use the ‘_ping_status’ and ‘_comments_status’ keys to set the status of the trackbacks and comments to ‘closed’, which effectively disables them.

Finally, we use the add_action function to hook the WPTurbo_auto_close_comments function into the publish_post action, which triggers the function whenever a post is published:

add_action( 'publish_post', 'WPTurbo_auto_close_comments' );

Register an account to save your snippets or go Pro to get more features.