WP comes with number of built-in functions that can be really useful. Particularly for developers who want's to perform certain operation on their site. The wp_schedule_event is one such function that allows you to do the same. It's more like the Cron job, if you know what I am talking about. So, let's take a look at the function.
<?php wp_schedule_event(time(),'hourly','things_todo'); ?>
As you can see from this function, it takes multiple parameters which you can definitely change based on your requirement. Instead of scheduling an event on "hourly" basis, you can perform them twice a day using "twicedaily" or once a day using "daily" value. Unfortunately, WP doesn't accept any other value as of today(you can do it custom though). At the end, you must refer to the function that should be performed.
Now, let's take a look at a full fledged sample function for better understanding.
<?php
// hook event register function with wordpress init
add_action('init','register_the_event');
// register the event
function register_the_event() {
// make sure this event hasn't been scheduled
if(!wp_next_scheduled('name_your_event')) {
// schedule a time on future date
$timestamp = strtotime('2017-05-20 24:00:00' );
// schedule the event
wp_schedule_event($timestamp,'daily','name_your_event');
}
}
// hook the event with the things to do
add_action('name_your_event','do_perform_this_task');
// perform this task on regular interval
function do_perform_this_task(){
// your magic code goes here
}
?>
What I am doing here is fairly simple and easy to understand. I wrote a function (register_the_event) and hooked it with the WordPress init hook action. This means my function will be called as soon as WordPress finishes loading. Within the register_the_event function we checked if our event has already been scheduled or not. If there is no such event exist, it will register our event (name_your_event) using wp_schedule_event function. Now, prior setting up the event, I passed a future date and time using $timestamp variable to start the event for the first time. It is not necessary but you may want to use it to define a specific time. Even though this time depends on the "Timezone" settings of your WP site. This can be found from "Settings -> General" option. You can simply use PHP time function here instead which will take UTC time. I just wanted to keep it local.
On the next section of my snippet, I added an action hook using the name of my event (name_your_event) which will call a function (do_perform_this_task) to perform certain operations. Inside this function you can write your own code to get your job done. Yes, you also call multiple others functions here as well. In that case you may end up with something like this.
<?php
// perform this task on regular interval
function do_perform_this_task(){
// task one
do_perform_task_one();
// task two
do_perform_task_two();
}
// actual task one
function do_perform_task_one(){
// task one operation
}
// actual task two
function do_perform_task_two(){
// task two operation
}
?>
You can get creative with this snippet as much as you want. This is particularly helpful if you want to perform certain tasks on regular interval. For an example, in one of my post I explained how to delete unnecessary post metadata that gets created quite often in WP. You can perform such operation using the schedule event function. You can also add function that needs to be performed on regular basis. Things like generating sitemap, product feed and stuff like that.
Note: Scheduling an event in WordPress is not like performing a real PHP based cron job as this function will work only if someone visits your site or you login to your site after the specified time or whenever WordPress gets initialized. This means, if there is not visitor on your site this event won't take place. You can simply visit your own site though just to get this job done. Hope that helps.
Comments