How to Setup a WordPress (WP) Cron Job

WordPress virtual cron job

A cron job is a task scheduled to run automatically at certain intervals.

By default, WordPress does not use a real cron job. Instead, whenever a page is loaded on a WordPress site, WordPress runs the wp-cron.php file. Although this “virtual” cron job is convenient, it has a few disadvantages:

  • Running the wp-cron.php file every time WordPress loads a page is unnecessary, this can affect server performance negatively, especially if your site gets a lot of traffic.
  • For a site with no high traffic, it can take some time before traffic (visitor) loads a page on your website (and triggers the cron task). This can cause missed schedules for publishing posts and other unintended effects.

To resolve these issues, you can disable the default virtual WordPress cron job, and configure a real cron job. Follow the steps below to achieve this.

Disabling the virtual WordPress cron job:

  1. Log in to your account using cPanel or SSH.
  2. Using the cPanel File Manager or the command line, open the wp-config.php file in a text editor.
    The wp-config.php file is located in the directory where you installed WordPress. Usually, this is the public_html directory.
  3. Add the following line to the wp-config.php file:
    define('DISABLE_WP_CRON', true);

    Make sure you add the previous line before the following line in the wp-config.php file:

    /* That's all, stop editing! Happy publishing. */
    
  4. Save the changes to the wp-config.php file and then exit the text editor. The virtual WordPress cron job is now disabled.

Configuring a real cron job for WordPress

After you have disabled the default WordPress cron configuration in the wp-config.php file, you can now setup a real cron job that runs at fixed intervals regardless of site traffic.

Method #1: Use cPanel
  1. Log in to cPanel.
  2. In the Advanced section of the cPanel home screen, click Cron jobs. Cpane > Cron
  3. Under Cron Email, type the e-mail address that you want to receive notifications, and then click Update Email. Every time the cron job runs, the e-mail account will receive a message.
    If you do not want to receive e-mail notifications for the cron job, you can append >/dev/null 2>&1 to the command, which redirects all output to /dev/null.
  4. Under Add New Cron Job, in the Common Settings list box, select Once per hour.
    You can run cron jobs a maximum of every 15 minutes on shared account. A 1-hour interval for the WordPress cron job should be more than sufficient.
  5. In the Command text box, type the following line:
    cd ${HOME}/public_html; /usr/local/bin/php -q wp-cron.php
    This line assumes that you installed WordPress in the document root (public_html) directory. If you installed WordPress in another directory, modify the cd command to change to that directory instead.
  6. Click Add New Cron Job. The new cron job settings take effect immediately.
Method #2: Use the command line
  1. Log in to your account using SSH.
  2. At the command prompt, type the following command:
    crontab -e
  3. Type o to enter editing mode and start a new line of text.
  4. Type the following line, replacing username with your own PadyHost.com Hosting account username:

    */30 * * * *  cd ${HOME}/public_html; /usr/local/bin/php -q wp-cron.php
    • This line sets the cron job to run every 30 minutes. If you are on an unmanaged platform you can run cron jobs as frequently as you want. On shared and reseller accounts, however, you can only run cron jobs a maximum of every 15 minutes. A 30-minute interval for the WordPress cron job should be more than sufficient.
    • This line assumes that you installed WordPress in the document root (public_html) directory. If you installed WordPress in another directory, modify the cd command to change to that directory instead.
  5. Press Esc, type :wq and then press Enter. The new cron job settings take effect immediately.
  • wordpress, cron
  • 1 brukere syntes dette svaret var til hjelp
Var dette svaret til hjelp?

Relaterte artikler

How to Reset my cPanel Account Password?

If you can access your cPanel account, follow these steps to reset your password:1. Log into your...

How to Update a Cronjob E-mail Address?

If you want to receive a cronjob error or notification, you can update your email address from...

How to create Cronjob via cPanel?

1. Log into your cPanel account. 2. In the Advanced section, click on Cron Jobs Icon. 3. Under...

How to check disk usage of directory and bandwidth usage?

If you are receiving a disk space usage warning and don't know which folder is using too much...

How to Edit file in the cPanel File Manager?

1. Log into your cPanel account. 2. In the Files section, click on File Manager Icon. 3. cPanel...