WordPress 定时发布失败解决办法

  • 酉灿
  • WordPress
  • Jul 23, 2021

经常有用户说定时发布文章是提示失败,造成失败原因不太清楚,应该是主机慢有关吧,我自己从没遇到过,这里记录一下解决办法,供大家参考。

推荐两款解决定时发布失败的插件:WP Missed Schedule Posts和MY Missed Schedule
 

也可以用下面的代码,代码应该是提取自老版本的WP Missed Schedule Posts插件,将代码添加到当前主题 functions.php 中:


 
  1. if (!function_exists( 'add_action' ) ) {
  2. header( 'Status 403 Forbidden' );
  3. header( 'HTTP/1.0 403 Forbidden' );
  4. header( 'HTTP/1.1 403 Forbidden' );
  5. exit();
  6. }
  7.  
  8. function wpms_log() {
  9. echo"\n";
  10. }
  11. add_action( 'wp_head', 'wpms_log' );
  12. add_action( 'wp_footer', 'wpms_log' );
  13.  
  14. define( 'WPMS_DELAY', 5 );
  15. define( 'WPMS_OPTION', 'wp_missed_schedule' );
  16.  
  17. function wpms_replace() {
  18. delete_option(WPMS_OPTION);
  19. }
  20.  
  21. register_deactivation_hook(__FILE__,'wpms_replace');
  22. function wpms_init() {
  23. remove_action('publish_future_post','check_and_publish_future_post');
  24. $last=get_option(WPMS_OPTION,false);
  25. if (($last!==false)&&($last>(time()-(WPMS_DELAY*60))))return;
  26. update_option(WPMS_OPTION,time());
  27. global$wpdb;
  28. $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");
  29. if (!count($scheduledIDs))return;
  30. foreach($scheduledIDs as$scheduledID) {
  31. if (!$scheduledID)continue;
  32. wp_publish_post($scheduledID);
  33. }
  34. }
  35. add_action( 'init', 'wpms_init', 0 );

代码添加后,定时发表文章还是会提示失败,但过二、三分钟后,会正常发布的。

至于哪个方法适合你,只能自己试了。

打赏