How to Schedule Events Using WordPress Cron Job

WordPress Cron

WordPress Cron is a virtual cron that only works when the page is loaded. WP-cron is first loaded by WordPress when a page is requested on the front or backend of a site at which point WP-cron displays the necessary page to the site visitor.

On low traffic sites this is perfectly fine, but when visitors roll in, checking multiple times for scheduled tasks can be very inefficient and lead to resource usage problems for your server, plus make your website load slower.

Linux Cron Job

Linux cron jobs can be controlled by the user, making certain that the items constrained by schedule are run appropriately. Also, for a high-traffic WordPress site, a Linux cron job can assist with reducing the chances of downtime by lowering the bandwidth on the server, thus using less server resources.

How to Replace WP-Cron With a Linux Cron Job

The best way to optimize the efficiency of your WordPress cron jobs is to disable WP-cron and set up a normal cron job through cPanel.

  • Disable default wp-cron.php behavior: We can easily tell WordPress to let us handle the execution of wp-cron.php with the wp-config.php file.
    1. Open your wp-config.php file with the cPanel File Manager Code Editor
    2. Go to the bottom of the database settings in wp-config.php
    3. Add this code : define('DISABLE_WP_CRON', 'true');
    4. Click Save
  • Setup a Linux cron job
    1. Log into your cPanel.
    2. In the Advanced section, click Cron jobs.
    3. Under Add New Cron Job, select the time interval you want.
    4. Set the cron command to the following, replacing with your actual domain name:
      wget -q -O - >/dev/null 2>&1
      The above command tells the Linux server to run wp-cron via wget, which will trigger the wp-cron.php script to do it's job on your schedule instead of on each page view.
    5. Click Add New Cron Job to set the cron.