*WordPress uses a file called wp-cron.php located in the root of the website as a virtual cron job (scheduled task) to automate things like publishing scheduled posts, checking for plugin or theme updates, sending email notifications, etc. By default, WordPress is set up to call wp-cron.php every time someone visits your WordPress website when a scheduled task is present.
On small/low traffic sites this isn't a problem, but when more visitors come to your website, checking multiple times for scheduled tasks can be very inefficient and lead to resource usage problems for the server, plus it will also make your website load slower.
Step 1: Disabling the default wp-cron.php behaviour
We can tell WordPress to let us handle the execution of wp-cron.php with the wp-config.php file.
- Open your wp-config.php file with the cPanel File Manager Code Editor
- Go to the bottom of the database settings in wp-config.php
- Add the code below highlighted in red:
/** The Database Collate type. Don't change this if in doubt. */
- Now click Save
Now WordPress will not automatically run the wp-cron.php script each time your site gets a new visitor.
Step 2: Setup a manual cron job for wp-cron.php
Now that the wp-cron.php is disabled we don't want to leave WordPress without any ability to automate tasks.
For most WordPress users having the wp-cron.php script run every 6 hours which is common setting. That would be 4 executions in a day, compared to possibly hundreds, or even thousands if you had a lot of website traffic that day.
- Log into cPanel
- Click on cron jobs (Under the Advanced section)
- Select Once per hour from the Common Settings drop-down
- Now select Every Sixth hours (*/6) from the Hour drop-down.
- Fill in cron job command click add new cron job
- Finally fill in the code to run our cron job and click Add New Cron Job.
cd /home/username/public_html; php -q wp-cron.phpReplace 'username' with your cPanel username.
Note: The path '/home/username/public_html' would be for a primary domain, if you're using an addon domain, or have WordPress installed in a sub-directory you'll want to be sure to update your path.
You should see that your new cron job was added successfully. Now your WordPress website should be safe running scheduled tasks via the wp-cron.php script, but only at set intervals.