wordpress 自动发布失败解决方法

网上说调整timeout的值大于0.01及安装WP Missed Schedule插件解决效果都不好。最后通过一篇文章了解到最有效的方法是直接把WP Missed Schedule里面的代码放到主题的functions.php文件中,创建cron.php文件 内容如下

<?php

if (!function_exists('add_action')) {
	header('Status 403 Forbidden');
	header('HTTP/1.0 403 Forbidden');
	header('HTTP/1.1 403 Forbidden');
	exit();
}

function wpms_log(){
	echo"\n<!--Plugin WP Missed Schedule 2011.0920.2011 Active-->";
}
add_action('wp_head', 'wpms_log');
add_action('wp_footer', 'wpms_log');

define('WPMS_DELAY', 5);
define('WPMS_OPTION', 'wp_missed_schedule');

function wpms_replace(){
	delete_option(WPMS_OPTION);
}
register_deactivation_hook(__FILE__, 'wpms_replace');

function wpms_init(){
	remove_action('publish_future_post', 'check_and_publish_future_post');
	$last = get_option(WPMS_OPTION, false);
	if(($last !== false) && ($last > (time() - (WPMS_DELAY*60)))) return;
	update_option(WPMS_OPTION, time());
	global $wpdb;
	$scheduledIDs = $wpdb->get_col("SELECT `ID` FROM `{$wpdb->posts}` WHERE(((`post_date`> 0) && (`post_date` <= CURRENT_TIMESTAMP())) OR ((`post_date_gmt`>0) && (`post_date_gmt` <= UTC_TIMESTAMP())) ) AND `post_status` = 'future' LIMIT 0,5");
	if (!count($scheduledIDs)) return;
	foreach ($scheduledIDs as $scheduledID) {
		if(!$scheduledID) continue;
		wp_publish_post($scheduledID);
	}
}
add_action('init', 'wpms_init', 0)
?>

将文件保存cron.php 放到主题的inc目录下,然后functions.php文件中插入如下代码

require get_template_directory() . '/inc/cron.php';
然后发布定时文章吧,测试过通常2、3分钟 文章也能发出去了,效果已经达到我想要的了。
发布了21 篇原创文章 · 获赞 3 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/zchare/article/details/80417844
今日推荐