OUR BLOG

Keep up with the latest web trends and Hosting UK news.

WordPress, your resource usage, and wp-cron.php

If you are looking to speed up WordPress, increase performance under load, or are running into issues with resource usage going absolutely crazy when a search engine indexes your site – then you may wish to switch from the “poor-mans-cron” approach and create a formal job to get WordPress to do its homework via wp-cron.php periodically, on your terms, and not with every page load. This article outlines the steps to take to achieve this.

WordPress uses a file called wp-cron.php to do all of its housework. Things such as checking for updates, sending out emails, tidying up, publishing articles, and so on. As the name suggests this is a way to get a Cron Job run – a familiar term for those of you are familiar with Linux/Unix/Apple iOS – however, for those of you more Windows-centric this would be a Scheduled Task.

As hosting is not always provided with the ability to set a Cron job or Scheduled tasks – Automattic has worked around this by applying what is often referred to as a ‘poor-mans-cron’ – that is to say, it runs the script to do the housework every time the page is called. This is a great idea – removes complication, but means that every time someone arrives at the site it calls the wp-cron.php and it makes with the “is it time yet?!”
Sometimes this works out great, sometimes it means a site will hang for a bit while it plays catch up, however, sometimes it really does start to slow things up and trip up the flow of things.

Disable wp-cron within wp-config

First of all, you are going to need to edit the wp-config.php file. If in doubt – make a backup first. Open the file in an editor of some description and find a suitable place somewhere under:

define('DB_COLLATE', '');

…and add the following line so that it looks like this:

define('DB_COLLATE', '');
define('DISABLE_WP_CRON', true);

Save the file.

It is important to complete the next step – otherwise, your WordPress will not do any housekeeping from this point onwards. The site will slow further, and it will not update searches, submit XML sitemaps, publish sites, and so on. This is suboptimal – so lets press on.

Create a cron job for wp-cron

You need to create a job now to run periodically using the control panel or the console. The control panel and the contents of any given cron are going to differ over time – however, this will give you a great idea of what needs to happen.

Plesk

What you need to know here is the absolute path of your code – this is usually your domain, but if you are not sure – please do ask. You will need to replace the ‘yoursubscriptionnamehere’ in the code below with this. Other than that – get logged in and off we go:

Ensure you have the correct SUBSCRIPTION selected top right;

On the right hand side of the WEBSITES & DOMAINS tab, look for the option SCHEDULED TASKS which has a calendar icon;

Click on ADD TASK in the light blue bar above any existing cron tasks;

Select RUN A PHP SCRIPT;

Within SCRIPT PATH fill out the following:

httpdocs/wp-cron.php

Select the latest version in the USE PHP VERSION drop down, so for example 5.6 or 7.0;

From the RUN drop down select HOURLY

DESCRIPTION – fill that out with something like “WP-CRON as a scheduled task”

NOTIFY – select Do not notify

Click on RUN NOW

Click on OKAY

….retire for tea and medals – your work here is done : )

cPanel

The following five steps will set up a cron for you within cPanel. All you need to know is your username (as you will need to replace the ‘yourusernamehere‘ in the code below with your user name), and access to your cPanel control panel.

Scroll down to the ADVANCED section, click on CRON JOBS (or search cron in the bar at the top);

Select once per hour from the COMMON SETTINGS.

Set the rest of the options to EVERY DAY EVERY MONTH and so on.

Now where it says COMMAND enter the following

cd /home/yourusernamehere/public_html; php -q wp-cron.php

Now click on ADD NEW CRON JOB

Your work here is done.

Console access

You have full hands-on access. You must have been very good in a former life – alas you also have the ability to make a big mess. Please note that the R key is very very close to the E key – it is sometimes wiser to do a crontab -l so you have a copy to hand should the unthinkable happen…

crontab -l
crontab -e

Now in your editor of choice – add the following line and comment for each site you are looking to have run wp-cron.php for. Needless to say you need to carefully consider the path you are going to use before opening the editor.

0 * * * * cd /path/to/my/website/document/root/here ; php -q wp-cron.php >/dev/null >2/dev/null

ESC :wq for anyone who is stuck in VI/M at this point 😉

So – you now have a record in there, and if it says its good – list the crontab again, copy paste the line to the command line, remove the -q and see what happens when you run it. For example:

cd /path/to/my/website/document/root/here ; php wp-cron.php

Returning to the console with no errors would suggest all was well. Flaming death – roll back the change in your wp-config and contact support.

Summary

This will now remove the burden of “on each page load run this” from your site content, and moved it to a periodic event.