Firstly, define your custom cron job schedules.
add_filter('cron_schedules', array($this, 'cron_schedules'));
public function cron_schedules($schedules){
$prefix = 'cron_';// Avoid conflict with other crons. Example Reference: cron_30_mins
$schedule_options = array(
'30_mins' => array(
'display' => '30 Minutes',
'interval' => '1800'
),
'1_hours' => array(
'display' => 'Hour',
'interval' => '3600'
),
'2_hours' => array(
'display' => '2 Hours',
'interval' => '7200'
)
);
/* Add each custom schedule into the cron job system. */
foreach($schedule_options as $schedule_key => $schedule){
$schedules[$prefix.$schedule_key] = array(
'interval' => $schedule['interval'],
'display' => __('Every '.$schedule['display'])
);
}
return $schedules;
}
You need to decide where and when to actually schedule the event.
Here is just an example snippet of code, which makes a call to a custom class method:
$schedule = $this->schedule_task(array(
'timestamp' => current_time('timestamp'), // Determine when to schedule the task.
'recurrence' => 'cron_30_mins',// Pick one of the schedules set earlier.
'hook' => 'custom_imap_import'// Set the name of your cron task.
));
Here is the code which actually schedules the event:
private function schedule_task($task){
/* Must have task information. */
if(!$task){
return false;
}
/* Set list of required task keys. */
$required_keys = array(
'timestamp',
'recurrence',
'hook'
);
/* Verify the necessary task information exists. */
$missing_keys = array();
foreach($required_keys as $key){
if(!array_key_exists($key, $task)){
$missing_keys[] = $key;
}
}
/* Check for missing keys. */
if(!empty($missing_keys)){
return false;
}
/* Task must not already be scheduled. */
if(wp_next_scheduled($task['hook'])){
wp_clear_scheduled_hook($task['hook']);
}
/* Schedule the task to run. */
wp_schedule_event($task['timestamp'], $task['recurrence'], $task['hook']);
return true;
}
Now, all you need to do is make a call to the name of your custom cron task. In this example the cron task name is custom_imap_import
.
add_action('custom_imap_import', array($this, 'do_imap_import'));
public function do_imap_import(){
// .... Do stuff when cron is fired ....
}
So in this example, $this->do_imap_import();
is called every 30 minutes (assuming you have enough traffic to your website).
Notes
Requires a page visit in order for your cron to fire at correct times.
Example: If you scheduled a task at 30 minute intervals, but no one visits your site for 4 hours, your cron job won't be fired until that visitor comes to your site 4 hours later. If you really truly need your task fired every 30 minutes, then it is advised to setup a legitimate cron job via your web hosting provider to visit your website at the desired intervals.
WordPress cron jobs don't make your website slow!
Maybe you are thinking what if the cron-script takes a long time to be executed, will the visitors have to wait until the script is executed. Nope! How can that be possible? If you look at the wp-cron.php
file you will find a line
ignore_user_abort(true);
It's a php.ini
configuration that sets that if you stop loading the site/script the script won't stop executing.
If you look at the wp-includes/cron.php
file you'll find a line like this:
wp_remote_post( $cron_url,
array('timeout' => 0.01,
'blocking' => false,
'sslverify' => apply_filters('https_local_ssl_verify', true)) );
That means WordPress will wait only 0.01 second for triggering the execution then it will abort but as you have set ignore_user_abort
to true
the script will be executing. This functionality is a huge advantage to execute large scripts in WordPress cron jobs.
Functions available for aid: