Title: spawn_cron
Published: April 25, 2014
Last modified: February 24, 2026

---

# spawn_cron( int $gmt_time ): bool

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/spawn_cron/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/spawn_cron/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/spawn_cron/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/spawn_cron/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/spawn_cron/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/spawn_cron/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/spawn_cron/?output_format=md#wp--skip-link--target)

Sends a request to run cron through HTTP request that doesn’t halt page loading.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/spawn_cron/?output_format=md#parameters)󠁿

 `$gmt_time`intoptional

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

## 󠀁[Return](https://developer.wordpress.org/reference/functions/spawn_cron/?output_format=md#return)󠁿

 bool True if spawned, false if no events spawned.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/spawn_cron/?output_format=md#source)󠁿

    ```php
    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 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.
    	 *         @type bool $sslverify Whether SSL should be verified for the request. Default false.
    	 *     }
    	 * }
    	 * @param string $doing_wp_cron The Unix timestamp (UTC) of the cron lock with microseconds.
    	 */
    	$cron_request = apply_filters(
    		'cron_request',
    		array(
    			'url'  => add_query_arg( 'doing_wp_cron', $doing_wp_cron, site_url( 'wp-cron.php' ) ),
    			'key'  => $doing_wp_cron,
    			'args' => array(
    				'timeout'   => 0.01,
    				'blocking'  => false,
    				/** This filter is documented in wp-includes/class-wp-http-streams.php */
    				'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
    			),
    		),
    		$doing_wp_cron
    	);

    	$result = wp_remote_post( $cron_request['url'], $cron_request['args'] );

    	return ! is_wp_error( $result );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/cron.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/cron.php#L871)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/cron.php#L871-L972)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/spawn_cron/?output_format=md#hooks)󠁿

 [apply_filters( ‘cron_request’, array $cron_request_array, string $doing_wp_cron )](https://developer.wordpress.org/reference/hooks/cron_request/)

Filters the cron request arguments.

 [apply_filters( ‘https_local_ssl_verify’, bool|string $ssl_verify, string $url )](https://developer.wordpress.org/reference/hooks/https_local_ssl_verify/)

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

## 󠀁[Related](https://developer.wordpress.org/reference/functions/spawn_cron/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_get_ready_cron_jobs()](https://developer.wordpress.org/reference/functions/wp_get_ready_cron_jobs/)`wp-includes/cron.php` |

Retrieves cron jobs ready to be run.

  | 
| [wp_redirect()](https://developer.wordpress.org/reference/functions/wp_redirect/)`wp-includes/pluggable.php` |

Redirects to another page.

  | 
| [wp_ob_end_flush_all()](https://developer.wordpress.org/reference/functions/wp_ob_end_flush_all/)`wp-includes/functions.php` |

Flushes all output buffers for PHP 5.2.

  | 
| [site_url()](https://developer.wordpress.org/reference/functions/site_url/)`wp-includes/link-template.php` |

Retrieves the URL for the current site where WordPress application files (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible.

  | 
| [wp_remote_post()](https://developer.wordpress.org/reference/functions/wp_remote_post/)`wp-includes/http.php` |

Performs an HTTP request using the POST method and returns its response.

  | 
| [get_transient()](https://developer.wordpress.org/reference/functions/get_transient/)`wp-includes/option.php` |

Retrieves the value of a transient.

  | 
| [set_transient()](https://developer.wordpress.org/reference/functions/set_transient/)`wp-includes/option.php` |

Sets/updates the value of a transient.

  | 
| [wp_unslash()](https://developer.wordpress.org/reference/functions/wp_unslash/)`wp-includes/formatting.php` |

Removes slashes from a string or recursively removes slashes from strings within an array.

  | 
| [add_query_arg()](https://developer.wordpress.org/reference/functions/add_query_arg/)`wp-includes/functions.php` |

Retrieves a modified URL query string.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  | 
| [is_wp_error()](https://developer.wordpress.org/reference/functions/is_wp_error/)`wp-includes/load.php` |

Checks whether the given variable is a WordPress Error.

  |

[Show 6 more](https://developer.wordpress.org/reference/functions/spawn_cron/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/spawn_cron/?output_format=md#)

| Used by | Description | 
| [_wp_cron()](https://developer.wordpress.org/reference/functions/_wp_cron/)`wp-includes/cron.php` |

Runs scheduled callbacks or spawns cron for all scheduled events.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/spawn_cron/?output_format=md#changelog)󠁿

| Version | Description | 
| [5.1.0](https://developer.wordpress.org/reference/since/5.1.0/) | Return values added. | 
| [2.1.0](https://developer.wordpress.org/reference/since/2.1.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fspawn_cron%2F)
before being able to contribute a note or feedback.