_wp_cron(): int|false
- Since
- 5.7.0
- Source
wp-includes/cron.php:1048
Description
Warning: This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. For information about casting to booleans see the https://www.php.net/manual/en/language.types.boolean.php PHP documentation. Use the === operator for testing the return value of this function.
Compatibility
- WordPress
- since 5.7.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Return value
int|false- On success an integer indicating number of events spawned (0 indicates no events needed to be spawned), false if spawning fails for one or more events.
Performance profile
How much work a call to _wp_cron() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Expensive
- Scaling
- Scales with input
- Instructions
- 5–60
- Plugin surface
- None
- Called by
- 1
Reaches an outbound HTTP request via wp_remote_post().
The body loops, so the work grows with how much data it finds.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 66.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()one call below _wp_cron() - transienttransient
get_transient()one call below _wp_cron() - httpoutbound HTTP request
wp_remote_post()one call below _wp_cron()
Further down the call graph this can also reach option, cache, serialize and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost _wp_cron() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 5–9 | none |
empty($crons) | 12–14 | wp_get_ready_cron_jobs() |
!empty($crons) && isset($keys) && $gmt_time | 25–27 | wp_get_ready_cron_jobs(), microtime(), array_keys() |
!empty($crons) | 31–41 | wp_get_ready_cron_jobs(), microtime(), array_keys(), wp_get_schedules() |
!empty($crons) && !$crons && !$gmt_time && !$cronhooks && !isset($value) | 49–55 | wp_get_ready_cron_jobs(), microtime(), array_keys(), wp_get_schedules(), spawn_cron() |
!empty($crons) && !$crons && !$gmt_time && !$cronhooks && isset($value) && call_user_func() | 54–60 | wp_get_ready_cron_jobs(), microtime(), array_keys(), wp_get_schedules(), call_user_func(), spawn_cron() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 66 | 5–60 | 14 | |
| 8.5 | 66 | 5–60 | 14 | |
| 8.4 | 66 | 5–60 | 14 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 72 | 8–66 | 14 | |
| 8.2 | 72 | 8–66 | 14 | |
| 8.1 | 72 | 8–66 | 14 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 73 | 9–67 | 14 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 4
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- wp_get_ready_cron_jobs()Retrieves cron jobs ready to be run.
- wp_get_schedules()Retrieves supported event recurrence schedules.
- spawn_cron()Sends a request to run cron through HTTP request that doesn't halt page loading.
Used by · 1
- wp_cron()Registers _wp_cron() to run on the {@see 'shutdown'} action.
Source code
function _wp_cron() { // Prevent infinite loops caused by lack of wp-cron.php. if ( str_contains( $_SERVER['REQUEST_URI'], '/wp-cron.php' ) || ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) ) { return 0; } $crons = wp_get_ready_cron_jobs(); if ( empty( $crons ) ) { return 0; } $gmt_time = microtime( true ); $keys = array_keys( $crons ); if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) { return 0; } $schedules = wp_get_schedules(); $results = array(); foreach ( $crons as $timestamp => $cronhooks ) { if ( $timestamp > $gmt_time ) { break; } foreach ( (array) $cronhooks as $hook => $args ) { if ( isset( $schedules[ $hook ]['callback'] ) && ! call_user_func( $schedules[ $hook ]['callback'] ) ) { continue; } $results[] = spawn_cron( $gmt_time ); break 2; } } if ( in_array( false, $results, true ) ) { return false; } return count( $results );}Changelog
Introduced in 5.7.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/cron.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.