wppaste
WordPress

_wp_cron(): int|false

Since
5.7.0
Source
wp-includes/cron.php:1048
Runs scheduled callbacks or spawns cron for all scheduled events.

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

Reaches an outbound HTTP request via wp_remote_post().

Scaling
Scales with input

The body loops, so the work grows with how much data it finds.

Instructions
5–60

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 66.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()one call below _wp_cron()
  • transienttransientget_transient()one call below _wp_cron()
  • httpoutbound HTTP requestwp_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.

WhenInstructionsCalls it makes
always5–9none
empty($crons)12–14wp_get_ready_cron_jobs()
!empty($crons) && isset($keys) && $gmt_time25–27wp_get_ready_cron_jobs(), microtime(), array_keys()
!empty($crons)31–41wp_get_ready_cron_jobs(), microtime(), array_keys(), wp_get_schedules()
!empty($crons) && !$crons && !$gmt_time && !$cronhooks && !isset($value)49–55wp_get_ready_cron_jobs(), microtime(), array_keys(), wp_get_schedules(), spawn_cron()
!empty($crons) && !$crons && !$gmt_time && !$cronhooks && isset($value) && call_user_func()54–60wp_get_ready_cron_jobs(), microtime(), array_keys(), wp_get_schedules(), call_user_func(), spawn_cron()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev665–6014
8.5665–6014
8.4665–60146 fewer instructions than PHP 8.3
8.3728–6614
8.2728–6614
8.1728–66141 fewer instruction than PHP 7.4
7.4739–6714

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

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 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.