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 you to write a PHP function and then run that function at a particular time, on 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 defaul 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 your 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
- You can manage crons with a plugin easily, such as WP Crontrol or Advanced Cron Manager.
- If you’re 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 your 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 your 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 your 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, you must 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 Password Protection because the site must be accessible.
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 your site, or your site needs
wp_cron to read as enabled, we have some additional troubleshooting steps to help determine the issue with your cron events.
First determine if the crons are failing, or if you just aren’t seeing the updates. If the cron process is functioning but you aren’t seeing updates, you may have a cache issue and not a cron issue.
- Locate your Apache access Logs
- Search for: wp-cron.php
- If you have crons running, you will see entries like the following:
200response code denotes the cron is working correctly.
502response code denotes a timeout. Optimize the cron process to complete under 60 seconds.
If you’ve confirmed wp-cron is not working on your site, we highly recommend reaching out to Support at this point for further guidance.
- If you are using a subdomain WordPress multisite, make sure each subdomain is added to the User Portal individually.
If you are able to use WP CLI, running
wp cron event list is another great way 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-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 your site password protected?
Check your error logs.
- It’s possible an error in the theme, plugin or core code is causing issues with the PHP code in your cron.