0815

Du musst dich anmelden um Beiträge und Themen zu erstellen. Einloggen · Registrieren

Automatically Disable Comments and Trackbacks in Old Posts

Here is yet another method for auto-disabling comments and trackbacks on old posts. This function automatically closes comments and trackbacks after the specified number of days. Add the following to your functions.php file:

<?php function autoclose_comments() {
	global $wpdb, $tableposts;

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

		$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'");
}

function autoclose_trackback() {
	global $wpdb, $tableposts;

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

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

Once in place, you may change the number of days allowed for comments and trackbacks to stay open by editing the two instances of the $age variable to whatever you wish. No other editing or code is required for this function to operate effectively. The function is executed automatically upon various posting actions. Note that this function is included in newer versions of WordPress (2.7 or better), and is therefore only required for older versions.

Source: WP Engineer   Plugin: Autoclose




0