Menu
in ,

How To Setting Up WordPress Cron? The Best Way

wordpress cron jobs events

If you are on Linux Server or VPS, the professional and best way to set up the WordPress cron job is to use the WP-CLI tool and the Linux crontab command, as discussed in this lab.

But if you were on a sharing hosting or do not have root access to your server, we will use one of the below suggestions of the WordPress Cron Job plugin control.

Install WP-CLI

We will run the following commands to install the WP-CLI. For more information about this tool, check the handbook.

If you are not a root login, you will need to start each of the following command lines with sudo.

# curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
# php wp-cli.phar --info
# chmod +x wp-cli.phar
# mv wp-cli.phar /usr/local/bin/wp

Setting up the WordPress Cron Job

In the typical case, when you need to run WP-CLI, wp we need to be in the WordPress files location or by the Apache language called the document-root, so we need to go to the document-root first and then fire the wp command.

For example, my WordPress website document-root and files are located in: /home/netslovers/public_html/ directory.

so to run the WordPress cron job WP command, we will do the following steps

# cd /path/to/wordpress/files/location
# wp cron event run --due-now --allow-root 

note that: we append –allow-root flag to run the wp command when we are root login, or skip this flag and remove it.

Same time we can run the wp command from any context, as we add it to the PATH variable location /usr/local/bin/.

So we can modify the above commands to be

# wp cron event run --due-now --path=/path/to/wordpress/files/location --allow-root

Append WordPress Cron Job To The Crontab

Now we need to append our WordPress cron job to the conrtab tool, by running the command

# crontab -e

Then we append one of the following command lines at the end of the crontab list

* * * * * /usr/local/bin/wp cron event run --due-now --path=/path/to/wordpress/files/location --allow-root > /dev/null 2>&1

OR

* * * * * cd /path/to/wordpress/files/location; /usr/local/bin/wp cron event run --due-now --allow-root > /dev/null 2>&1

This corn job will run every minute, as we set its schedule * * * * *, but to run every hour set the schedule to 0 * * * *, and to set a custom cron schedule you can use this cron schedule expressions online tool.

It's now safe to disable the WordPress standard cron jobs event, by appending the defined line in the wp-config.php config file.

define('DISABLE_WP_CRON', true);

Setting Up WordPress Cron Job In Sharing Hosting

If you are on cPanel, you can set up your WordPress cron job using the advanced section cron jobs

And set the schedule in the Cron job form by picking up one of the predefined schedules in the common settings select list.

And insert the corn command as the following

wget -q -O - https://your-website-domain-name.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1

Then add your corn job.

But if you are on shared hosting and go to depend on the embed WordPress cron job, you will need to use additional plugins to make sure your system runs smoothly.

Suggestions WordPress plugins:

1- Scheduled Post Trigger: You may schedule your posts for a specific to publish time/date, this plugin helps to avoid any mistakes in the Posts publishing queue that depends on WordPress Cron.

2- WP Crontrol: Enables you to view and control the WordPress Cron Job events system.

Leave a Reply

Exit mobile version