spawn_cron( int $gmt_time = 0 ): bool
- Since
- 2.1.0, 5.1.0
- Source
wp-includes/cron.php:899
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
- Scaling
- Constant
- Instructions
- 5–93
- Plugin surface
- 2 hooks
- Called by
- 1
Reaches an outbound HTTP request via wp_remote_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 140.
Third-party callbacks on 'cron_request', 'https_local_ssl_verify' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- transienttransient
get_transient()called directly - hookthird-party callbacks
apply_filters()called directly - httpoutbound HTTP request
wp_remote_post()called directly - cacheobject cache
wp_cache_get()one call below spawn_cron() - optionoption read or write
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 5–8 | none |
| always | 9–12 | microtime() |
!defined('DOING_CRON') && !isset($value) && $gmt_time | 22–23 | get_transient() |
!defined('DOING_CRON') && !isset($value) && $gmt_time | 26–27 | microtime(), get_transient() |
!defined('DOING_CRON') && !isset($value) && !$gmt_time && empty($crons) | 27–28 | get_transient(), wp_get_ready_cron_jobs() |
!defined('DOING_CRON') && !isset($value) && !$gmt_time && empty($crons) | 31–32 | microtime(), get_transient(), wp_get_ready_cron_jobs() |
!defined('DOING_CRON') && !isset($value) && !$gmt_time && !empty($crons) | 36–49 | get_transient(), wp_get_ready_cron_jobs(), array_keys() |
!defined('DOING_CRON') && !isset($value) && !$gmt_time && !empty($crons) | 40–53 | microtime(), 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–82 | 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) && defined('ALTERNATE_WP_CRON') && !defined('DOING_AJAX') && !defined('XMLRPC_REQUEST') | 82–86 | microtime(), 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) | 83–89 | 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() |
!defined('DOING_CRON') && !isset($value) && !$gmt_time && !empty($crons) | 87–93 | microtime(), 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: 140 instructions, 5–93 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:
- apply_filters( https_local_ssl_verify )filterline 993 (+94 into the body)
Filters whether SSL should be verified for local HTTP API requests.
Uses · 11
- get_transient()Retrieves the value of a transient.
- wp_get_ready_cron_jobs()Retrieves cron jobs ready to be run.
- set_transient()Sets/updates the value of a transient.
- wp_redirect()Redirects to another page.
- add_query_arg()Retrieves a modified URL query string.
- wp_unslash()Removes slashes from a string or recursively removes slashes from strings within an array.
- wp_ob_end_flush_all()Flushes all output buffers for PHP 5.2.
- site_url()Retrieves the URL for the current site where WordPress application files (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_remote_post()Performs an HTTP request using the POST method and returns its response.
- is_wp_error()Checks whether the given variable is a WordPress Error.
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 ); $cron_url = add_query_arg( 'doing_wp_cron', $doing_wp_cron, site_url( 'wp-cron.php' ) ); /** * 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 string $key The Unix timestamp (UTC) of the cron lock with microseconds. * @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.Changelog
Introduced in 2.1.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.