Hey buddies, its been a long while since I wrote my last article. I have been really caught up getting myself up to date with technology, learning some new things and reviewing the code of our various products. (After all I want you guys to get the best possible products). Nevertheless, now I’m back. So, without further ado let’s get down to today’s post on WordPress Crons.
There might surely have been a time when you must have required to run some specific code periodically. Obviously, you’re not going to run the code yourself every time it is required to be done. That’s where the concept of Cron comes into the picture.
So, what is a Cron?
- A cron is a command that enables you to schedule a job to be executed at some later point of time.
- Generally crons are set at system level. That means you need to have access to SSH or cPanel in order to set a cron.
- Depending upon the client you are working with, you may or may not get access to cPanel or SSH and in that case if you want to run some WordPress related code periodically, then you can go for a WordPress cron.
This post is not centered around how you should setup a WordPress cron or about the code required to setup a WordPress cron. There exist tons of tutorials explaining that. The one I like referring to is here.
Today, we are going to see what exactly happens after you have written the code to schedule a cron job. Let’s start!
WordPress Cron Execution: Lifecycle
Step 1: Init Hook is Fired
- WordPress has lots of hooks and filters. In the early execution of hooks, there is a hook named ‘init’. You will find do_action( ‘init’ ); in wp-settings.php.
- This hook is fired after WordPress has finished loading. Till this time nothing is rendered to the screen (i.e. headers are not rendered) and this hook is executed on every refresh.
Step 2: wp_cron Function is Called
- On this hook, WordPress calls the function ‘wp_cron’. How? By writing add_action( ‘init’, ‘wp_cron’ ) in wp-includes/default-filters.php
- So technically, when anyone comes to your website (or visits a page on your website), init hook is fired which in turn fires ‘wp_cron’ function. This function is defined in wp-includes/cron.php
Step 3: WordPress Cron status is checked
- The wp_cron function checks the status of the WordPress cron (i.e. it checks whether constant DISABLE_WP_CRON is defined or not). If the WordPress cron is not disabled, then it obtains the list of all crons from database.
- All crons that have been obtained are stored in database in wp_options table with option ‘cron’.
Step 4: Arrange Cron Jobs According to Timestamp
- The next step in this process is to get a list of crons to be run. This step is processed using the get_option(‘cron’) command.
- The array returned by get_option(‘cron’) is arranged in timestamp order. Timestamp is the key in that array.
- So the job (function) to be run first is found first in that list.
Step 5: Cron Job is Executed
- WordPress checks the timestamp of the first key. If that timestamp is beyond the current time, it calls the function spawn_cron. This function is also defined in includes/cron.php
- This spawn_cron function makes sure that another cron is not running and Cron is not locked.
- Further, it also checks if the constant ‘ALTERNATE_WP_CRON’ is set. You have to manually set this constant in wp-config.php file. Generally, it is used if WordPress cron is not working at all on your wordpress website. When this constant is set to true, visitors are redirected to the page they are on but their url is appended with query parameter ‘doing_wp_cron’. During that time wp-cron.php is included and executed. To know about when to set ‘ALTERNATE_WP_CRON’, read here.
- If ‘ALTERNATE_WP_CRON’ constant is not set, WordPress sends POST request to wp-cron.php. i.e. it calls wp-cron.php as external link.(So if your site is example.com, it sends POST request to http://example.com/wp-cron.php). It does not wait for the response to get from wp-cron.php. (Actually, it waits for 0.01 seconds. Virtually, it has triggered a separate request to wp-cron.php) and resumes to the original flow. Thats how wp-cron.php is executed along with actual WordPress loading and hence WordPress cron does not affect the page load time
So in brief, the flow is like this, on every refresh WordPress checks if there is any cron job that needs to be executed. If there is, it just sends POST request to wp-cron.php and without waiting for that request’s response it resumes to the original flow.
I hope, this clears the working of WordPress Cron. If you have any questions or any inputs that would be beneficial for our readers please feel free to use our comments section.