Created
August 14, 2020 05:22
-
-
Save junaidtk/941c9cb8b1b12eab2d90014a85d50a45 to your computer and use it in GitHub Desktop.
How to create a basic Cron job scheduling in WP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WP-Cron Jobs | |
============= | |
Wp-Cron handles scheduling time-based tasks in WordPress. Several WordPress core features, such as checking for updates and publishing scheduled post, utilize WP-Cron. | |
WP-Cron works by checking, on every page load, a list of scheduled tasks to see what needs to be run. Any tasks due to run will be called during that page load. | |
Scheduling errors could occur if you schedule a task for 2:00PM and no page loads occur until 5:00PM. | |
With WP-Cron, all scheduled tasks are put into a queue and will run at the next opportunity (meaning the next page load). So while you can’t be 100% sure when your task will run, you can be 100% sure that it will run eventually. | |
WP-Cron is given two arguments: the time for the first task, and an interval (in seconds) after which the task should be repeated. For example, if you schedule a task to begin at 2:00PM with an interval of 300 seconds (five minutes), the task would first run at 2:00PM and then again at 2:05PM, then again at 2:10PM, and so on, every five minutes. | |
To simplify scheduling tasks, WordPress offers three default intervals and an easy method for adding custom intervals. | |
The default intervals provided by WordPress are: | |
hourly | |
twicedaily | |
daily | |
weekly (since WP 5.4) | |
To add a custom interval, you can create a filter, such as: | |
add_filter( 'cron_schedules', 'example_add_cron_interval' ); | |
function example_add_cron_interval( $schedules ) { | |
$schedules['five_seconds'] = array( | |
'interval' => 5, | |
'display' => esc_html__( 'Every Five Seconds' ), ); | |
return $schedules; | |
} | |
This filter function creates a new interval that will allow us to run a cron task every five seconds. | |
To scheule a atsk wordpress uses below function | |
if ( ! wp_next_scheduled( 'bl_cron_hook' ) ) { | |
wp_schedule_event( time(), 'five_seconds', 'bl_cron_hook' ); | |
} | |
$timestamp – The UNIX timestamp of the first time this task should execute | |
$recurrence – The name of the interval in which the task will recur in seconds | |
$hook – The name of our custom hook to call | |
You can add the function to be run on this schedule by using the default wordpress hook functionality. | |
add_action( 'bl_cron_hook', 'bl_cron_exec' ); | |
function bl_cron_exec(){ | |
write_log(time()); | |
write_log('junaid TK'); | |
} | |
function write_log ( $log ) { | |
if ( true === WP_DEBUG ) { | |
if ( is_array( $log ) || is_object( $log ) ) { | |
error_log( print_r( $log, true ) ); | |
} else { | |
error_log( $log ); | |
} | |
} | |
} | |
So the function bl_cron_exec will be executed during each five_seconds intervell. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment