WordPress cron job practice

WordPress cron job best practice

In WordPress all scheduled events are controled by wp-cron.php file.

How cron job work in WordPress

The file wp-cron.php is the part of WordPress that handles scheduled events within a WordPress site. Anything that has to do with scheduling posts or publications and really anything date/time oriented is governed by the wp-cron.php file.
In order for wp-cron.php to work properly, it needs to be executed frequently, but no more than once per minute. However, the default behavior does not require you to set up a real system level cron job on your server. Instead, it uses a piggyback method on every incoming request. When a request comes into the site, WordPress will generate an additional request from itself to the wp-cron.php file over HTTP(S). It is invoked by http(s) each request. If no any request all events would never run. What if a large number of requests in a short period of time? It would become DDOS attack.

Alternatives

The best practice for scheduled jobs is to setup system cron job on server side that executes the wp-cron.php script directly through PHP in certain interval. This ensures that any scheduled tasks are indeed executed at their scheduled time.

Cron functions

Create cron hook

add_action( 'hostlike_cron_hook', 'wp_cron_function' );

Schedule a task

wp_schedule_event ( time(),  'hourly',  'hostlike_cron_hook' );  //may register duplicate event
wp_next_scheduled ( 'hostlike_cron_hook' );   //return next execution timestamp, it check if a particular hook is already scheduled to ensure the event only be registerred once

Recurring event

if ( ! wp_next_scheduled( 'hostlike_cron_hook' ) ) {
    wp_schedule_event( time(), 'hourly', 'hostlike_cron_hook' );
}

Unscheduling

register_deactivation_hook( __FILE__, 'wp_deactivate' );

function wp_deactivate() {
    $timestamp = wp_next_scheduled( 'hostlike_cron_hook' );
    wp_unschedule_event( $timestamp, 'hostlike_cron_hook' );
}

Disable default cron

In wp-config.php file, which located in the root folder of the website.

Add the following line to disable the default cron function:

define('DISABLE_WP_CRON', true); /* disable wpcron scheduled job handler must be placed after DB_COLLATE */

disable-cron

Find the php file location

From the result of phpinfo(), find “Configure Command” string, the prefix is the location.

phpinfo

Add server cron job

Once the default cron function being disabled, all scheduled jobs will never be run. So we have to add server cron job.
Goto cpanel -> cron jobs

command line: cd /usr/local/php71/; php /home/hostlike/public_html/wp-cron.php

Set Twice Per Hour (0,30 * * *)
cron-job-cpanel

All scheduled events would be controled by this setting. Any job with recurring inteval less than this setting will not work as the interval set.

View all scheduled jobs

In order to view all scheduled jobs you need to install plugin like WP Control, and activate it.
wp-control-plugin
Go to Tools -> Cron Events
cron-events

Creating custom job

  1. Create plugin
  2. add cron hook
    add_action( 'hostlike_cron_hook', 'check_expiry' );
  3. schedule the hook
    if ( ! wp_next_scheduled( 'hostlike_cron_hook' ) ) {
      wp_schedule_event( time(), 'hourly', 'hostlike_cron_hook' );
    }

    Or, from WP Control plugin
    add-cron-event
    Note that the event interval should be greater than the server cron scheduled interval, in this case, greater than 30 minutes.

Cron in database

All cron information is stored in wp_options table with option_name = 'cron'. The value look like this:

i:1579966219;                                 //next run time. 1579966219 = '2020-01-25 09:30:19'
a:1:{s:18:"hostlike_cron_hook";               //hook name
a:1:{s:32:"40cd750bba9870f18aada2478b24840a"; //guid id
a:3:{s:8:"schedule";s:5:"daily";s:4:"args";   //schedule info
a:0:{}s:8:"interval";i:86400;}}}              //run interval. 86400 = 24 hours

 

282 total views, 3 views today

Author: Albert

Leave a Reply