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.
There are two ways to leverage this:
- Within a page or a post within WordPress, you can 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.
While WordPress does have some scheduling capabilities, some 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 then 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 checks for due now crons even minute. This interval cannot be changed. 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 your environment.
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 );
The WP Engine Alternate Cron does not work for sites that are utilizing Password Protection.
NOTE: Alternate cron requires
wp_cron be set to false, which is disabled. This may be important and conflict with other custom functionality.
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 Access Logs in the User Portal, these are near the Error Logs
- Ensure you’re viewing the Apache access logs and not the Nginx access logs
- Search for: wp-cron.php
- If you have successful crons, you should see something 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.