Do your scheduled posts never publish or your backups never start?
In this lunchtime #WPQuickies, I talk about how to set up a cron job in cPanel to make sure your scheduled posts and backups never fail.
What Is a Cron Job?
A cron job allows you to automate commands or scripts on your site that require being triggered at regular intervals or whenever specific events need to be scheduled; one-time, daily, weekly, monthly or a custom interval.
Why WP-Cron Sucks
WordPress handles scheduling in a bizarre way through a built-in service called WP-Cron. It sucks.
WP-Cron is not a true scheduler because it doesn’t check for jobs every x seconds.
Instead, WP-Cron is triggered whenever somebody visits your site.
That’s right, if nobody visits your website, WP-Cron will not run and therefore no scheduled events will happen.
Have you ever had a post that failed to be published on a schedule or a backup that never ran. That’s WP-Cron for you!
High-traffic websites may not have this problem but for smaller websites with less traffic this can be a nightmare 🙁
So, what’s the solution?
Server Cron Jobs
The server that runs your website (mostly a Linux type operating system) uses a service called cron and unlike WordPress this service runs all the time.
With a modest bit of effort you can create a server cron job to ping WordPress and tell it to run any scheduled events – this cron job never misses a beat.
Disable WordPress’ WP-Cron
The first thing to do is to disable WordPress’ WP-Cron – we don’t want WordPress to be in charge of running the schedule any more.
Add the following code into your WordPress configuration file wp-config.php, near the top of the file.
define( ‘DISABLE_WP_CRON’, true );
Cron Jobs in cPanel and Plesk
Setting up a cron job in cPanel or Plesk is easy, they have tool for that in the Advanced box in cPanel and to the RHS of your main Plesk window.
Let’s have a look at setting this up in cPanel.
Cron Jobs in cPanel
The cron job interface allows you to specify any interval you want:
Minute: The number of minutes between each time the cron job runs, or the minute of each hour on which you wish to run the cron job.
Hour: The number of hours between each time the cron job runs, or the hour of each day on which you wish to run the cron job.
Day: The number of days between each time the cron job runs, or the day of the month on which you wish to run the cron job.
Month: The number of months between each time the cron job runs, or the month of the year in which you wish to run the cron job.
Weekday: The days of the week on which you wish to run the cron job.
But let’s keep this easy and use a common setting using the Common Settings dropdown.
Choose an interval from the Common Settings drop-down menu.
I like to use the once per five minutes option.
The settings are pre-populated for you. In this case our cron job will run every 5 minutes of every hour, of every day, of every month of every weekday – basically every 5 mins forever until stopped.
The last thing to do is to enter a command that you want the cron job to run.
In this case we’re going to ping WordPress’ WP-Cron file wp-cron.php to force it to look for and run scheduled events.
We enter the following command,
wget -q -O - https://yourdomain.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
Replacing yourdomain.com with your own website domain name, then click on Add New Cron Job – all done!
Let’s look a bit closer at that command.
This command split over two lines just to fit it on the screen for you to see but it all goes on the one line.
Wget is a command line program that retrieves content from web servers.
There are three parameters:
-q (lowercase q) turns on quiet mode turning off any output to the terminal
-O – (uppercase letter O) tells wget not to output anything to the file system
https://yourdomain.com/wp-cron.php?doing_wp_cron This is the web command we want to run to ping the WP-Cron system of your website. Replace https://yourdomain.com/ with your own website domain name.
The last bit of the command is not part of wget but a system method for redirecting terminal output.
>/dev/null tells the system not to display any output and 2>&1 tells the system not to display any errors out to the terminal.
Still Not Working?
If your cron job is still not working, check that the URL https://yourdomain.com/wp-cron.php?doing_wp_cron is working for your own domain.
Create and schedule a test post for 5 minutes ago in your current time, then enter that URL into your web browser.
It should show a blank screen with no errors and publish your post.
If that is working then you have setup something else wrong in the cron job.
You can edit the cron job and enter the information again, look through the server log for errors or send a support ticket to your hosting provider, but I never have any issues with setting up a cron job using this method.
Replacing the stuffy, old, unreliable WP Cron with a server cron job ensures all your WordPress scheduled features such as scheduled posts and backups always run like clockwork.
Join me every Thursday at 1 pm Sydney time for some more WPQuickies – WordPress tips and tricks in thirty minutes or less.
Broadcasting live on YouTube and Facebook.
Suggest a #WPQuickies Topic
If you have an WordPress topic you’d like to see explained in 30 mins or under, fill out the form below.