the wp_schedule_event. This post is about wp_schedule_single_event.
wp_schedule_single_event is for scheduling a single event but you can use it to execute a function more than once.
wp_schedule_single_event( $timestamp, $hook, $args = array())
$timestamp is the timestamp when we want to execute our function
$hook is the hookname(a string) that we will attach our function with.
$args is optional parameter WordPress uses to make a md5 hash string to make the schedule unique
If you want to execute your function 'my_cron_function' at the timestamp= 'timestamp' follow the code
add_action('my_hook','my_cron_func'); function my_cron_func(){ //do something here } wp_schedule_single_event('timestamp', 'my_hook')you can use different timestamps with your hook 'my_hook' , but remember that
WordPress checks if an identical event is to be executed in 10 minutes, if so WordPress won't execute the current event.
if you use
wp_schedule_single_event(time(), 'my_hook') wp_schedule_single_event(time()+20, 'my_hook');WordPress will discard the second one because it's scheduled to execute to in 10 minutes of the previous identical one.
see the code the the wp-includes/cron.php
if ( $next && $next <= $timestamp + 600 ) return;Executing the function more than once using wp_schedule_single_event! if you use some dynamic value in the $timestamp you can make this execute more than once. for example If you use this code
wp_schedule_single_event(time()+60, 'my_hook')when you first activate the plugin the code schedules to execute the function 1min after the current time if any visitor visits the page if you use
wp_schedule_single_event(time(), 'my_hook')it will execute the function immediately. If you refresh your page in one minute WordPress won't execute the cron job as WordPress has set the default time interval between two cron jobs to 1 minute look for the code the wp-includes/cron.php
/* don't run if another process is currently running it or more than once every 60 sec. */ if ( $flag + 60 > $local_time ) return;so if you want any cron job to execute maximum time you can use
wp_schedule_single_event(time(), 'my_hook');another way to do this is to use
wp_schedule_event(time(),'every_minute','my_hook');you have make the 'every_minute' schedule yourself see the example Here
No comments:
Post a Comment