一段代码轻松解决wordpress定时发布失败的问题

  • 时间:2020-05-17 12:16:18
  • 分类:网络文摘
  • 阅读:132 次

之前,本站写过一篇关于 wordpress 文章发布出现“定时发布失败”的原因及解决方法 的文章。其中介绍了定时发布失败的原因和通过插件或更改 wordpress 程序文件解决问题的方法。但如果你的网站服务器过于垃圾,前文中的方法或许仍不能奏效。今天再来介绍一段代码,可以轻松解决这个问题。

将如下代码添加到你的wordpress主题的 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. function wpms_log() {
  8.     echo"\n";
  9. }
  10. add_action( 'wp_head', 'wpms_log' );
  11. add_action( 'wp_footer', 'wpms_log' );
  12. define( 'WPMS_DELAY', 5 );
  13. define( 'WPMS_OPTION', 'wp_missed_schedule' );
  14. function wpms_replace() {
  15.     delete_option(WPMS_OPTION);
  16. }
  17. register_deactivation_hook(__FILE__,'wpms_replace');
  18. function wpms_init() {
  19.     remove_action('publish_future_post','check_and_publish_future_post');
  20.     $last=get_option(WPMS_OPTION,false);
  21.     if (($last!==false)&&($last>(time()-(WPMS_DELAY*60))))return;
  22.     update_option(WPMS_OPTION,time());
  23.     global$wpdb;
  24.     $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");
  25.     if (!count($scheduledIDs))return;
  26.     foreach($scheduledIDs as$scheduledID) {
  27.         if (!$scheduledID)continue;
  28.         wp_publish_post($scheduledID);
  29.     }
  30. }
  31. add_action( 'init', 'wpms_init', 0 );

说明:此段代码应该是提取自老版本的wordpress插件 WP Missed Schedule Posts ,定时发布有时会出现二分钟左右的延迟,之后会正常发布的。

推荐阅读:
What Should Your Anti Virus Software Have in Order to Be Effecti  The Importance of SEO and How They Improve the Number of Your Cl  Learn The Basics Of Google My Business  Iterative and Recursion Algorithms to Compute the Number of Step  How to Check if a DLL or EXE is 32-bit or 64-bit (x86 or x64) us  C++: How to Iterate the Elements over the Sets (set, unordered_s  How to Iterate over the Items in Java’s Map (HashMap, Hash  Dynamic Programming Algorithm to Count Vowels Permutation  Exploring the Importance of Brand Consistency  Cleaning A WordPress Malware Infection For Dummies 
评论列表
添加评论