Have you sometimes noticed that your WordPress installation take a few extra seconds to load? Or maybe that your scheduled posts fail to publish on time? This maybe due to your the WordPress cron function.
What is a Cron Job?
As stated on Wikipedia (http://en.wikipedia.org/wiki/Cron) The software utility Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals.
In layman terms this is a program you can access through control panel to ensure that another program does the same thing at a given time period.
What is a WordPress Cron?
WordPress comes with it's own Cron which enables you to schedule posts or automate events with WordPress. This is part of the core features and is triggered when a visitor browses to your website and navigates through the pages.
So if WordPress has it's own cron where is the problem? Well, there are numerous issues that can arise for why the process doesn't complete.
This can range from:
- Your site doesn't receive visitors when needed
- DNS mis-configuration
- Server Overloaded
- Caching plugins stopping wp-cron.php from firing.
- and the list goes on
This means that if WordPress cron doesn't get triggered at the right time, the post doesn't schedule. Also as this is adding an additional program to run when your site is visited this can add to the load time as WordPress needs to go through an automation tasks before it can get to rendering the page for you.
How to replace WordPress Cron (wp-cron.php) with an actual Cron Job?
Once logged into your web hosting control panel, navigate to the "Advanced" section at the bottom or CPanel and click on Cron Jobs.
Within the Cron Job panel you will be able to add a new task or view/amend existing tasks.
Adding a new task
The settings available to trigger a task are:
* * * * * command to execute
│ │ │ │ │
│ │ │ │ │
│ │ │ │ └───────────────── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
│ │ │ └─────────────────── month (1 - 12)
│ │ └───────────────────── day of month (1 - 31)
│ └─────────────────────── hour (0 - 23)
└───────────────────────── min (0 - 59)
Note: In Cron Jobs "*" means "every"
When adding a new task you can select pre-defined time schedules from the first drop-down list, but we need something slightly different.
We need the process to trigger every 15 minutes which needs us to select "Once per 15 Minutes (*/15)" from the second drop down list, then in each of the other boxes we just need * (Every).
We will use the "wget" command which is a free software package for retrieving files using HTTP, HTTPS and FTP, the most widely-used Internet protocols. It is a non-interactive command-line tool, so it may easily be called from scripts, cron jobs, terminals without X-Windows support, etc.
The code that we need to execute is
wget -q -O - http://example.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
You will need to change example.com to the specific location to your WordPress installation.
Now that we have set-up the server to trigger your site, we need to stop WordPress triggering it each time a visors browses to your site.
Within the web hosting control panel scroll to the top of the page and click "Home", the navigate to the "File Manager" from the top "Files" section.
This gives the options to navigate to different parts of your web hosting account. For our purpose we need to select "Web Root", which will open in a new tab.
Within the new tab the page splits into three sections:
- Top: File Operation
- Left: Folder navigation
- Right: Files within the selected folder
Note:If you have installed WordPress to a sub-folder i.e. example.com/wordpress you will need to navigate to this folder in the left hand section
Once you have found the root folder of your WordPress installation you need to single click to select "wp-config.php" in the right hand section.
Next select "Code Editor" from the Top file operations section; Again this will open in a new tab.
The file that you have just opened contains some crucial settings for your WordPress to work so make sure that you don't change anything!
We need to navigate to the bottom of the file and add:
How to test
Set-up a new test post that is scheduled for 20 mins in the future.....Wait...... If WordPress has automatically published the post then everything has worked as planned. If not, run through the tutorial again, or log a support ticket and we will look into it for you.