• code
  • wordpress
  • heroes
  • community
  • developers
  • inspire
Loaded
Post Image

In this entry, we will go through how the cron system works on WordPress. We´ll analyse from scratch the functionality, so we can execute scheduled tasks in the CMS.

Using a cron or scheduled task will give you the possibility of executing a certain coding/function in a reccurrent way within a fix periodicity. For instance, you can trigger a task every hour that may send an email as a reminder to your users.

Handling the recurrence values by default in WordPress

The WordPress core has several definitions of default task ranges to facilitate development. Particularly, WP uses three crons that are very frequent in web page building:

  • Task execution as hourly
  • Task execution as daily
  • Task execution as twice-daily

As a beginning practice, we´ll execute a daily task. Basically, we´ll integrate a code that sends an email each day.

Code for executing through daily cron
function wph_cron_schedule() {
  if (!wp_next_scheduled('wph_cron_daily')){
    wp_schedule_event(time(), 'daily', 'wph_cron_daily');
  }
  /*wp_unschedule_event(wp_next_scheduled('wph_cron_five_minutes'), 'wph_cron_five_minutes', array());*/
}
add_action('wp', 'wph_cron_schedule');

function wph_cron_daily_function() {
  wp_mail('info@wordpress-heroes.com', 'Cron Worked', date('r'));
}
add_action('wph_cron_daily', 'wph_cron_daily_function');

function wph_cron_schedule() {
  if (!wp_next_scheduled('wph_cron_daily')){
    wp_schedule_event(time(), 'daily', 'wph_cron_daily');
  }

  /*wp_unschedule_event(wp_next_scheduled('wph_cron_five_minutes'), 'wph_cron_five_minutes', array());*/
}
add_action('wp', 'wph_cron_schedule');

function wph_cron_daily_function() {
  wp_mail('info@wordpress-heroes.com', 'Cron Worked', date('r'));
}
add_action('wph_cron_daily', 'wph_cron_daily_function');

In the code we establish a cascade of events to link our functions with the WordPress core. First we associate the wph_cron_schedule function with the wp action. This allows us to later develop in our function the code that will be executed in WordPress loading.

In this case we´ll create a programming through the function wp_schedule_event. Just, we order our system with a conditional that if it isn´t scheduled the event based on wph_cron_daily function, then it will be stored in the system from now on.

What WordPress does is store this wph_cron_daily function that we develop later to run it every day from the moment it is saved. That is to say that every day as of today WordPress would execute the code that is in the wph_cron_daily function at the same time. Precisely, WordPress is holding this function wph_cron_daily that we have developed to execute it each day from the moment it is stored. So to say, WordPress will execute this code daily at the same hour that is found in the function wph_cron_daily.

We must keep in mind that WordPress works over code level, sothe cron will be executed with the condition that the server loads it. To be able to perform this execution, to the page must be visited. If this visit does not occur, then the cron won´t be executed either.

There are other systems that are based on the internal clock of the server that is hosted by web page. This modality is much more tricky and it is out of our scope . However, generally with the cron system of WordPress it should be sufficient and cover the majority of scheduled tasks on your installations.

Resuming, WordPress will execute the code of our functionality wph_cron_daily function every day at the same hour. On our example, it just sends an email throgh wp_mail function. It is so smooth and easy, but imagine all you can acomplish with this system.

One more detail. you will also find wp_unschedule_eventfunction as commented. This function exactly removes the scheduled task of your database in order to stop executions. This function turns out to be really interesting in terms of cleaning outdated the scheduled functions that you would not like to be executed on your installation.

Creating a new cron on WordPress

Maybe you would like to establish the frequency of sending mails beyond from what WordPress offers you by default. Let´s suppose that you need your function to be executed in each 5 minutes.

It is not possible to add a reccurence of 5 minutes. You would have only option of sending it hourly, daily, twice a day as mentioned previously.

To achieve this, first of all we have to establish a new interval of time that WordPress can interprete.

Code for creating a new interval for scheduled tasks

function wph_cron_five_minutes_schedule($schedules) {
  $schedules['wph_five_minutes'] = array(
    'interval' => 300,
    'display' => __('Every 5 minutes', 'wph-heroes')
  );
  return $schedules;
}
add_filter('cron_schedules','wph_cron_five_minutes_schedule');

function wph_cron_five_minutes_schedule($schedules) {
  $schedules['wph_five_minutes'] = array( 'interval' => 300, 'display' => __('Every 5 minutes', 'wph-heroes'));
  return $schedules;
}
add_filter('cron_schedules','wph_cron_five_minutes_schedule');

The wph_cron_five_minutes_schedule function will be in charge of adding arrays schedules to the core of new interval through two elements. The interval in seconds, in this case 300 seconds would be a equivalent of 5 minutes; and the tag under WordPress will save this code.

Once we have changed the array of our system code and we have included our new customized period of 5 minutes, now we are ready to use it.

We´ll do it with the exact same way of how we have done it before. Now we have to keep in mind that instead of using WordPress autodefined tags, we have to apply our new definition.

Code for executing through customized cron
function wph_cron_schedule() {
  if (!wp_next_scheduled('wph_cron_five_minutes')){
    wp_schedule_event(time(), 'wph_five_minutes', 'wph_cron_five_minutes');
  }
}
add_action('wph_cron_five_minutes', 'wph_cron_five_minutes_function');

function wph_cron_five_minutes_function() {
  wp_mail('info@wordpress-heroes.com', 'Five Minutes Cron Worked', date('r'));
}

function wph_cron_schedule() {
  if (!wp_next_scheduled('wph_cron_five_minutes')){
    wp_schedule_event(time(), 'wph_five_minutes', 'wph_cron_five_minutes');
    }
  }
add_action('wph_cron_five_minutes', 'wph_cron_five_minutes_function');

function wph_cron_five_minutes_function() {
  wp_mail('info@wordpress-heroes.com', 'Five Minutes Cron Worked', date('r'));
}

As you can see the code is equivalent to the one we used above for time programming. In this case, the identification tag of the cron that we just created wph_cron_five_minutes is modified. And it also changes of course the function that is associated with this tag and with this new cron wph_cron_five_minutes_function.

Prev
Create a Talking Avatar with HTML5 and JQuery
Next
Customize dashboard users list in WordPress

close
close
close
close

Task removal

The task will be completely deleted. This process cannot be reversed and the task cannot be recovered.

[userswph-login]
[userswph-user-register-form]
close
close

Warning: Undefined array key "post_type" in /home/u232964957/domains/wordpress-heroes.com/public_html/wp-content/plugins/whitepaper-manager-wph/templates/pprwph-footer.php on line 28
close
close
close