通过WP Cron定时删除修订日志

WP Cron是WordPress定时任务不可缺少的功能,可以通过其定时删除修订日志,虽然Easy WP Cleaner也非常好用,但是我太懒,不想点,想自动、定时删除功能。

直接上代码,functions.php中加入:

/**
 * Set cron to delete post revisions.
 *
 */
// add filter to allow for new weekly schedule
add_filter( 'cron_schedules', 'rkv_weekly_cron' );

function rkv_weekly_cron( $schedules ) {
	// Adds once weekly to the existing schedules.
	$schedules['weekly'] = array(
		'interval' => 604800,
		'display' => __( 'Once Weekly' )
	);
	return $schedules;
}

add_action('rkv_revision_cron', 'rkv_revision_delete');

// set cron schedule
function rkv_revision_schedule() {
	if ( !wp_next_scheduled( 'rkv_revision_cron' ) ) {
		wp_schedule_event( time(), 'weekly', 'rkv_revision_cron');
	}
}
add_action('wp', 'rkv_revision_schedule');

// run deletion on revisions
function rkv_revision_delete() {

	// query revisions and get array of IDs
	$args = array(
		'fields'        => 'ids',
		'post_type'     => 'revision',
		'numberposts'   => -1
	);

	$revisions = get_posts($args);

	// loop through and delete each
	foreach ($revisions as $revision):

		wp_delete_post( $revision );

	endforeach;
}

这样就添加成功了,可以通过WP Crontrol插件来查看定时任务:

WP Cron定时删除修订日志
WP Cron定时删除修订日志

发表回复

您的电子邮箱地址不会被公开。