Tips for WordPress Cron functions

WordPress provides some nice APIs for executing unattended scheduled tasks without relying on any external cron! Usually, we need to create a cron from our control panel / systems! However, if you implement built in cron features of WordPress, you gain better portability of your WordPress site!

However, it has a great limitation! This cron fully relies on site hits! WordPress triggers its cron functions after someone visits the site! When anyone visits your site, WP checks if any cron are supposed to be executed on or earlier of that moment! If no one visits your site, WordPress will fail to execute your tasks!

In this blog, I am going to show two tips that may be helpful while you work with wp-cron functions!


Custom Recurrences

By default, WordPress provides 3 recurrences settings! They are ‘hourly’, ‘daily’ and ‘twice daily’! However, what if you need to execute something at different intervals? No worry! WordPress developers are very intelligent! They provided APIs for custom recurrence intervals!

Let’s assume we want to execute a cron each 3 days! Then we have to use its filter api to do it!

add_filter('cron_schedules','custom_cron_schedules');

We have attached a function to the cron_schedules filter! Now let’s define our function to use our own recurrence frequency!

function custom_cron_schedules($schedules){
$schedules['threedays'] = array(
'interval'   => '259200',
'display'   => __('Each 3 Days'),
);

return $schedules;
}

Your function must take $schedules as parameter! Otherwise, default (and custom intervals by other plugins) will not work! In the function, we have added an associative array whose key is ‘threedays’! This is important! Because when you want to use this recurrence interval, you must use this name! Then we have set interval = 259200! It is the product of (3 days x 24 hours x 60 minutes x 60 seconds)! For example, if you want a job to be executed at every 10 minutes, the interval will be 600! That means, the interval will be the number of seconds of the period! We have also defined another key ‘display’ that will be used for displaying only! Finally return the $schedules array!

Your definition is done! Now its time to use this interval! When you set a cron, you define this way:

wp_schedule_event(time(),'threedays','our_cron_hook');

Note:
For real usages, it is not done actually! It just defined the hook recurrences interval! Now you need to define which function is attached with this hook! You can do that by the following line:

add_action('our_cron_hook','execute_cron_jobs');

Now, your instructions to wordpress is completed! It’s time for your play! Now you have to make ‘execute_cron_jobs’ function however you want! This function will be called once every 3 days and do the intended tasks, if you did not do any mistake ;)!

Clearing all cron functions attached to your hook
When you want to remove a cron from WordPress, usually during uninstall of the plugin, you can simply write as follow

wp_unschedule_event(time(), 'our_cron_hook');

It is good enough! However, in my experience during development period, I have observed (I explained how, later) that several crons are created and attached to this hook! I still don’t know why! So, I wanted to clear them all at once, during the un-installation of the plugin! That is also simple! Instead of the previous unschedule function, use it:

wp_clear_scheduled_hook('our_cron_hook');

It will remove all the functions attached to your hook and all scheduled tasks! Good, isn’t it?

One bonus tip
How can you really check if your hook is really scheduled at WordPress? Yes, you can see it very easily! For viewing cron schedules, I use a plugin named Cron View! That will list all the schedules and their next occurrence time! Install it right now and then you know if you are really good at WordPress Crons ;)!

For a complete understanding of WordPress cron, you may read this article! More Information

  • frustphil

    Thanks mate! so helpful 🙂