Event Scheduling and wp-cron
WordPress® has a built in system for running scheduled events called the WordPress Cron, or wp-cron for short.1 This allows PHP functions to run at a particular time, at a particular frequency.
About wp-cron
The default WordPress cron functionality, called wp-cron, checks on each page load for scheduled tasks that are due to run. This means wp-cron is not a true server cron and fires solely based on traffic to the website.
To leverage default wp-cron functionality there are a few options:
- Within a page or a post within WordPress, set a publish date using WordPress’ Schedule Event feature.
- Add a new cron event by using the wp_schedule_event function within the theme or plugin code.
- There are some code examples given in the WordPress Codex article.
- Schedule an event using WP CLI.
- A full list of
wp cron event
commands can be found here.
- A full list of
While WordPress does have some scheduling capabilities, many website managers prefer a more comprehensive toolset for scheduling events. WP Crontrol and Advanced Cron Manager are two examples of cron plugins that some of our customers have used successfully.
Technical Information
- After creation, a cron event is added to the
wp_options
table. - Manage crons with a plugin easily, such as WP Crontrol or Advanced Cron Manager.
- If using WP CLI, view crons by running
wp cron event list
and find a full list of cron commands here. - WordPress triggers
wp-cron.php
when any page of the website is visited. This process queries the list of wp-crons for any that are “due now” and executes them.
Missed Cron
WordPress will check for scheduled events on every page visit, rather than every second (like a true server cron). This generally works fine, but it can cause problems in two ways:
- If a website doesn’t get much traffic, crons will not be triggered to run very often. Crons will only run when someone visits the website. This is the most common reason that a website’s event misses its scheduled time.
- If a website gets a lot of traffic, the process to check for crons will be triggered very frequently. This causes the server to work harder than it really needs to and can cause slowness, timeouts, and other performance issues on the website.
WP Engine Alternate Cron
Missed crons and crons that run too frequently can both be resolved by enabling the WP Engine Alternate Cron. This alternate cron is a service on the server itself and checks for “due now” crons every minute by curling wp-cron.php, rather than running cron processes based on traffic flow.
The alternate cron can be used to mimic a true server cron, even though WP Engine does not technically support true server crons. Alternate cron runs at a consistent minute interval and can be used to ensure processes run at their required time.
Alternate cron can be enabled within your User Portal by following these steps:
- From the Sites page, select the Environment you wish to enable Alternate Cron on
- Click Utilities in the secondary menu on the left (You may have to expand the Advanced dropdown section)
- Click the Alternate cron toggle switch to turn it on
Alternate cron requires wp_cron
be set to false, which means that the WordPress default cron must be disabled. This may conflict with other custom functionality on the website, so testing may be necessary after initially enabling the feature. To disable WordPress cron, alternate cron sets the following define in the website’s wp-config.php
file and confirms this define is set daily through server processes.
define( 'DISABLE_WP_CRON', true );
Simply adding disabling wp cron within the environment’s config file will not enable WP Engine alternate cron on its own. Be sure to follow the steps above to properly enable alternate cron for the environment.
Websites using WP Engine’s Password Protection will work correctly with our alternate cron. However, alternate cron does not work for websites that are utilizing other forms of custom password protection (basic auth, etc) because it must remain publicly accessible.
Troubleshooting
If enabling alternate cron doesn’t work for the website, or the website needs wp_cron
to read as enabled, there are some additional troubleshooting steps to help determine the issue with cron events.
First determine if the crons are failing, or if the updates simply aren’t showing. If the cron process is functioning but updates aren’t displaying, there may be a cache issue and not a cron issue.
If crons are failing to run:
- Locate the Site Access Logs
- Search for: wp-cron.php
- If the website has crons running, there will be entries like the following:
200
response code denotes the cron is working correctly.502
response code denotes a timeout.- To correct, optimize the cron process to complete under 60 seconds.
If you’ve confirmed wp-cron is not working on the website, but are still unsure, we highly recommend reaching out to Support at this point for further guidance.
Advanced Troubleshooting
- Confirm the domain added in the User Portal for this website matches what is mapped within WordPress.
- If using a subdomain multisite, try adding each subdomain to the User Portal individually.
- If possible, use WP CLI to
wp cron event list
to see if crons are stuck or not.- Crons that are scheduled to next_run: now should be updating if cron is functioning as expected.
- If crons are not updating, try running
wp cron event run --due-now
to see if the process gets hung up anywhere. Crons need to fire sequentially, if there’s an error with a previous cron, the following cron may not run.
- Is the website password protected?
- Webites using basic authentication will not work with wp-cron. This includes transferable sites.
- Check the website’s error logs.
- It’s possible an error in the theme, plugin, or core code is causing issues with the PHP code in the cron.
NEXT STEP: Learn how to diagnose and troubleshoot a 502 error