After some time working with WordPress, as you gain more knowledge about this awesome tool, you’ll hit some advanced concepts that you may be were not aware that existed.
I’m pretty sure that, after some time, you’ll hit the WP-Cron. But what does it mean? How do I use it? Is it a monster? How can I create my own cron job?
These are a few questions that we are going to answer in this post. So, grab your coffee, tea, and a chocolate and enjoy this post!
What is a cron job?
The idea behind the cron is that a few tasks need to be run once in a while, or in intervals. But from a user point of view, it doesn’t make sense to repeat tasks from time to time, so the developer created the cron, which creates a task (usually using the command line) and then schedule this task for a specific time.
The Wikipedia says that:
The software utility cron is a time-based job scheduler in Unix-like computer operating systems. Users that set up and maintain software environments use cron to schedule jobs to run periodically at fixed times, dates, or intervals.
So, to summarize, a cron job is a scheduled event that will happen when it’s needed. Do not forget this piece of information.
Give me examples
“Ok, I understood that this is an event scheduler, but I’m not a party promoter, so I’m not scheduling anything. How can I use that? Is it for me?”
My dear friend, events are more present in your life than you can imagine!
If you are a content creator, do you remember when you schedule a post to be published in the next day? Well, if you are using WordPress, then this is an example of cron job.
You are creating a task (post publishing) that will be ran in the next day (cron job). Boom! You are using cron jobs without knowing about it!
But there are many other ways to use it, like emailing your users once a week, send your posts to Google Drive every day, check for updates daily, and so on.
How does cron jobs work with WordPress?
I’ve copied some information that is available on WordPress.org and that you can even check by yourself. I copied that because the WP.org team made a huge effort to make it simple to understand – and that’s what we look for too.
WP-Cron is how WordPress handles scheduling time-based tasks in WordPress. Several WordPress core features, such as checking for updates and publishing scheduled post, utilize WP-Cron. The “Cron” part of the name comes from the cron time-based task scheduling system that is available on UNIX systems.
WP-Cron works by checking, on every page load, a list of scheduled tasks to see what needs to be run. Any tasks due to run will be called during that page load.
Unlike a traditional system cron that schedules tasks for specific times (e.g. “every hour at 5 minutes past the hour”), WP-Cron uses intervals to simulate a system cron.
In case you want to read more about it, you can access the link below:
How can I see the scheduled events on my website?
I’m pretty sure there are a few events running on your website under the hood, so, in case you want to check them, the best way to do that is by installed a WordPress plugin that will show you all the scheduled events for you.
The most used one is called WP Control. With this plugin, you can view all the cron events running on your website, and you can even create your own scheduled event.
If you don’t know how to install a plugin, we’ve created a quick guide for you. You can check it here.
Can you create an example of cron job?
Yeah, for sure! As an example, we’ll check for updates every hour (it’s just an example, you don’t need to check it so frequently. Once or twice a day should be fine).
In the image above we have a few events that are scheduled to run on my website. You can see that there a version check (2nd line), and there is a plugin update check in the 3rd line, that runs twice daily. We’ll change it!
To edit it, you can hover over the action (wp_update_plugins) and click on edit. Then, the next page will be the following:
On this page you’ll be able to change the hook name and the arguments (both are for advanced users). You’ll also be able to select when you want to run the next check AND now change the recurrence.
As per the screenshot, you can see I changed it from twice daily to once hourly. It means that my WordPress will look for updates every hour!
Isn’t it amazing?
This is the basics of WP-Cron and cron jobs. If you’d like to go further, you’ll need to spend some time reading the WordPress documentation about it to create some custom code. If you think it’s important for you, please comment below and we can create tutorials with code, so you can see how it works too.
WP-Cron is a powerful tool you can make use on your WordPress. If you are a developer, it’s an awesome tool to use in your projects if you need to check for specific things. If you are a user, it’s good that you learn what is happening behind the scenes on your website.