Pet*_*ter 17 php wordpress cron
有关此主题的stackoverflow上有很多帖子但是(我不知道为什么)没有什么对我有用.
是)我有的
function isa_add_every_three_minutes( $schedules ) {
$schedules['every_three_minutes'] = array(
'interval' => 180,
'display' => __( 'Every 3 Minutes', 'textdomain' )
);
return $schedules;
}
add_filter( 'cron_schedules', 'isa_add_every_three_minutes' );
function every_three_minutes_event_func()
{
// do something
}
wp_schedule_event( time(), 'every_three_minutes', 'every_three_minutes_event_func' );
Run Code Online (Sandbox Code Playgroud)
我有什么想法吗?
Shi*_*hur 25
您需要使用add_action()将函数挂钩到预定事件.
add_action( 'isa_add_every_three_minutes', 'every_three_minutes_event_func' );
Run Code Online (Sandbox Code Playgroud)
这是完整的代码.
// Add a new interval of 180 seconds
// See http://codex.wordpress.org/Plugin_API/Filter_Reference/cron_schedules
add_filter( 'cron_schedules', 'isa_add_every_three_minutes' );
function isa_add_every_three_minutes( $schedules ) {
$schedules['every_three_minutes'] = array(
'interval' => 180,
'display' => __( 'Every 3 Minutes', 'textdomain' )
);
return $schedules;
}
// Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'isa_add_every_three_minutes' ) ) {
wp_schedule_event( time(), 'every_three_minutes', 'isa_add_every_three_minutes' );
}
// Hook into that action that'll fire every three minutes
add_action( 'isa_add_every_three_minutes', 'every_three_minutes_event_func' );
function every_three_minutes_event_func() {
// do something
}
?>
Run Code Online (Sandbox Code Playgroud)
好像你忘了使用命令add_action()
请参阅此示例:http://wpguru.co.uk/2014/01/how-to-create-a-cron-job-in-wordpress-teach-your-plugin-to-do-something-automatically/