wppaste
WordPress

spawn_cron( int $gmt_time = 0 ): bool

Since
2.1.0, 5.1.0
Source
wp-includes/cron.php:851
Sends a request to run cron through HTTP request that doesn't halt page loading.

Compatibility

WordPress
since 5.1.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.

Parameters

$gmt_timeintoptional
Unix timestamp (UTC). Default 0 (current time is used).Default: 0

Return value

bool
True if spawned, false if no events spawned.

Performance profile

How much work a call to spawn_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
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
5–91

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

Plugin surface
2 hooks

Third-party callbacks on 'cron_request', 'https_local_ssl_verify' run inside this call, and their cost is not bounded by anything here.

Called by
1

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

What it touches

  • transienttransientget_transient()called directly
  • hookthird-party callbacksapply_filters()called directly
  • httpoutbound HTTP requestwp_remote_post()called directly
  • cacheobject cachewp_cache_get()one call below spawn_cron()
  • optionoption read or writeget_option()one call below spawn_cron()

Further down the call graph this can also reach 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 · 12 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost spawn_cron() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always5–8none
always9–12microtime()
!defined('DOING_CRON') && !isset($value) && $gmt_time22–23get_transient()
!defined('DOING_CRON') && !isset($value) && $gmt_time26–27microtime(), get_transient()
!defined('DOING_CRON') && !isset($value) && !$gmt_time && empty($crons)27–28get_transient(), wp_get_ready_cron_jobs()
!defined('DOING_CRON') && !isset($value) && !$gmt_time && empty($crons)31–32microtime(), get_transient(), wp_get_ready_cron_jobs()
!defined('DOING_CRON') && !isset($value) && !$gmt_time && !empty($crons)36–49get_transient(), wp_get_ready_cron_jobs(), array_keys()
!defined('DOING_CRON') && !isset($value) && !$gmt_time && !empty($crons)40–53microtime(), get_transient(), wp_get_ready_cron_jobs(), array_keys()
!defined('DOING_CRON') && !isset($value) && !$gmt_time && !empty($crons) && defined('ALTERNATE_WP_CRON') && !defined('DOING_AJAX') && !defined('XMLRPC_REQUEST')78–82get_transient(), wp_get_ready_cron_jobs(), array_keys(), sprintf(), set_transient(), ob_start(), wp_unslash(), add_query_arg(), wp_redirect(), wp_ob_end_flush_all(), flush()
!defined('DOING_CRON') && !isset($value) && !$gmt_time && !empty($crons)81–87get_transient(), wp_get_ready_cron_jobs(), array_keys(), sprintf(), set_transient(), site_url(), add_query_arg(), apply_filters(), timeout(), wp_remote_post(), is_wp_error()
!defined('DOING_CRON') && !isset($value) && !$gmt_time && !empty($crons) && defined('ALTERNATE_WP_CRON') && !defined('DOING_AJAX') && !defined('XMLRPC_REQUEST')82–86microtime(), get_transient(), wp_get_ready_cron_jobs(), array_keys(), sprintf(), set_transient(), ob_start(), wp_unslash(), add_query_arg(), wp_redirect(), wp_ob_end_flush_all(), flush()
!defined('DOING_CRON') && !isset($value) && !$gmt_time && !empty($crons)85–91microtime(), get_transient(), wp_get_ready_cron_jobs(), array_keys(), sprintf(), set_transient(), site_url(), add_query_arg(), apply_filters(), timeout(), wp_remote_post(), is_wp_error()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 138 instructions, 5–91 executed per call, 13 branches. The work does not change between versions.

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.

Hooks and filters fired · 2

2 hooks fire while spawn_cron() runs, in this order:

  1. apply_filters( cron_request )filterline 934 (+83 into the body)

    Filters the cron request arguments.

  2. apply_filters( https_local_ssl_verify )filterline 943 (+92 into the body)

    Filters whether SSL should be verified for local HTTP API requests.

Uses · 11

Used by · 1

  • _wp_cron()Runs scheduled callbacks or spawns cron for all scheduled events.

Source code

function spawn_cron( $gmt_time = 0 ) {	if ( ! $gmt_time ) {		$gmt_time = microtime( true );	} 	if ( defined( 'DOING_CRON' ) || isset( $_GET['doing_wp_cron'] ) ) {		return false;	} 	/*	 * Get the cron lock, which is a Unix timestamp of when the last cron was spawned	 * and has not finished running.	 *	 * Multiple processes on multiple web servers can run this code concurrently,	 * this lock attempts to make spawning as atomic as possible.	 */	$lock = (float) get_transient( 'doing_cron' ); 	if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS ) {		$lock = 0;	} 	// Don't run if another process is currently running it or more than once every 60 sec.	if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time ) {		return false;	} 	// Confidence check.	$crons = wp_get_ready_cron_jobs();	if ( empty( $crons ) ) {		return false;	} 	$keys = array_keys( $crons );	if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {		return false;	} 	if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) {		if ( 'GET' !== $_SERVER['REQUEST_METHOD'] || defined( 'DOING_AJAX' ) || defined( 'XMLRPC_REQUEST' ) ) {			return false;		} 		$doing_wp_cron = sprintf( '%.22F', $gmt_time );		set_transient( 'doing_cron', $doing_wp_cron ); 		ob_start();		wp_redirect( add_query_arg( 'doing_wp_cron', $doing_wp_cron, wp_unslash( $_SERVER['REQUEST_URI'] ) ) );		echo ' '; 		// Flush any buffers and send the headers.		wp_ob_end_flush_all();		flush(); 		require_once ABSPATH . 'wp-cron.php';		return true;	} 	// Set the cron lock with the current unix timestamp, when the cron is being spawned.	$doing_wp_cron = sprintf( '%.22F', $gmt_time );	set_transient( 'doing_cron', $doing_wp_cron ); 	/**	 * Filters the cron request arguments.	 *	 * @since 3.5.0	 * @since 4.5.0 The `$doing_wp_cron` parameter was added.	 *	 * @param array $cron_request_array {	 *     An array of cron request URL arguments.	 *	 *     @type string $url  The cron request URL.	 *     @type int    $key  The 22 digit GMT microtime.	 *     @type array  $args {	 *         An array of cron request arguments.	 *	 *         @type int  $timeout   The request timeout in seconds. Default .01 seconds.	 *         @type bool $blocking  Whether to set blocking for the request. Default false.	 *         @type bool $sslverify Whether SSL should be verified for the request. Default false.	 *     }

Changelog

Introduced in 2.1.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.

5.1.0
Return values added.from the docblock
2.1.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 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.