Follow

How to set up cronjob for WP Cron

About WP Cron

WordPress Cron (WP Cron, wp-cron.php) is the internal task scheduler in WordPress website. It is used to run WordPress scheduled tasks. Many WP plugin are relying on WP Cron to run their backend functions.

By default WP Cron is triggered by website visits: every time a visitor load your website, there is a small chance that your WP Cron is triggered, and it'll execute on scheduled. This may not be good for you:

- The website load speed will be decreased, as it must execute backend functions.

- It eats up your server resources e.g. CPU cycles, memory, etc, as it needs to check the scheduled task list every time it's triggered.

- It's not stable, as it depends on website visits. If there is no visits for some time, the backend tasks will not be executed.

- It's not on time. If you want some functions to execute at an exact time, you'd better use a decent cronjob service like SetCronJob.

To set up cronjobs for WP Cron, follow these step by step:

Disabling WP Cron

  1. Open your wp-config.php file with a file manager (e.g. cPanel file manager)
  2. Go to the bottom of the database settings in wp-config.php typically around line 37.

    Add the code below highlighted in red:

    /** The Database Collate type. Don't change this if in doubt. */
    define('DB_COLLATE', '');
    
    define('DISABLE_WP_CRON', 'true');
    
  3. Click Save

 Set up cronjob at SetCronJob

  1. Log in to your SetCronJob account
  2. Click Add a cronjob 
  3. Enter the URL to call: http://yoursite.com/wp-cron.php?__random__ (replace http://yoursite.com/ with your actual WordPress homepage URL)
  4. Select When to call: Every hour
  5. Click Save changes

and you're done!

You may notice that I added ?__random__ into the cronjob URL. It'll add a random number every time your cronjob is executed, so it'll prevent your web server from caching the HTTP request. 

0 Comments

Please sign in to leave a comment.