我很难做到这一点并检查过以前的问题,但它们似乎没有起作用 .
到目前为止,我通过在我的wp-config.php中添加以下内容来禁用默认的wordpress cron:
define('DISABLE_WP_CRON', true);
然后我试图安排我的任务从插件主php文件中每隔5分钟运行一次:
function my_cron_schedules($schedules){
if(!isset($schedules["5min"])){
$schedules["5min"] = array(
'interval' => 5*60,
'display' => __('Once every 5 minutes'));
}
if(!isset($schedules["30min"])){
$schedules["30min"] = array(
'interval' => 30*60,
'display' => __('Once every 30 minutes'));
}
return $schedules;
}
add_filter('cron_schedules','my_cron_schedules');
function schedule_my_cron(){
wp_schedule_event(time(), '5min', 'fivemin_schedule_hook');
}
if(!wp_get_schedule('fivemin_schedule_hook')){
add_action('init', 'schedule_my_cron',10);
}
function fivemin_schedule_hook() {
get_feed();
}
所以上面似乎是在数据库中安排我的事件,但在检查cron时间表时有100个条目:
<?php print_r(get_option('cron')); ?>
我还确保用以下内容更新我的crontab:
* * * * * wget -q -O - http://wordpress.com/wp-cron.php?doing_wp_cron
但是,我的任务似乎没有运行,并担心这个5分钟工作的数据库中的条目数量 .
每个条目如下所示:
Array ( [1524308364] => Array ( [fivemin_schedule_hook] => Array ( [40cd750bba9870f18aada2478b24840a] => Array ( [schedule] => 5min [args] => Array ( ) [interval] => 300 ) ) )
我尝试调试wp-cron.php,当它试图触发时回显$钩子,当我直接访问wp-cron.php时显示我的钩子 . 然而,实际的功能似乎并没有激发 .
1 回答
试试这个:
替换这个:
..有了这个:
但更合适/首选的方法是在插件的激活功能中添加:
例:
..将用于代替以下内容:
并在插件的停用功能中添加此内容:
例:
有关详细信息,请参阅https://codex.wordpress.org/Function_Reference/wp_schedule_event#Examples .