Event Scheduling and wp-cron
WordPress has a built in system for running scheduled events called the WordPress Cron, or wp-cron for short. This allows PHP functions to run at a particular time, at a particular frequency.
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 UNIX cron and actually runs more or less often based solely on traffic.
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 eventcommands 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.
- After creation, a cron event is added to the
- 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 listand find a full list of cron commands here.
- WordPress triggers
wp-cron.phpwhen any page of the site is visited. This process queries the list of wp-crons for any that are “due now” and executes them.
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 site doesn’t get much traffic, crons will not be triggered to run very often. Crons will only run when someone visits the site. This is the most common reason that a site’s event misses its scheduled time.
- If a site 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 site.
WP Engine Alternate Cron
Both of these problems can be resolved by enabling WP Engine Alternate Cron. To enable Alternate Cron, simply reach out to WP Engine Support.
Alternate cron is a service on the server that checks for “due now” crons every minute.
Additionally, alternate cron can be used to mimic a server cron because WP Engine does not support true linux, or server side, crons. Alternate cron runs at a consistent minute interval and can be used instead to run other processes that require a schedule.
Alternate cron sets the following define in the site’s
wp-config.php file and confirms it’s still set through our daily server processes.
define( 'DISABLE_WP_CRON', true );
Simply adding this define will not enable WP Engine alternate cron, reach out to Support to ensure the server configuration option is toggled on for each environment.
WP Engine’s alternate cron does not work for sites that are utilizing custom password protection (basic auth, etc) because the site must be accessible. Sites using WP Engine’s Password Protection will work correctly with our alternate cron.
Alternate cron requires
wp_cron be set to false, which means it will be disabled. This may be important and conflict with other custom functionality, so testing may be necessary after initially enabling the feature.
If enabling alternate cron doesn’t work for the site, or the site 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 site has crons running, there will be entries like the following:
200response code denotes the cron is working correctly.
502response 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 site, but are still unsure, we highly recommend reaching out to Support at this point for further guidance.
- Confirm the domain added in the User Portal for this site matches what is mapped within WordPress.
- If using a subdomain WordPress multisite, try adding each subdomain to the User Portal individually.
- If possible, use WP CLI to
wp cron event listto 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-nowto 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 site password protected?
- Check the site’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.