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

---

# wp_cron()

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_cron/?output_format=md#description)
 * [Source](https://developer.wordpress.org/reference/functions/wp_cron/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_cron/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_cron/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_cron/?output_format=md#user-contributed-notes)

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

Registers [_wp_cron()](https://developer.wordpress.org/reference/functions/_wp_cron/)
to run on the [‘shutdown’](https://developer.wordpress.org/reference/hooks/shutdown/)
action.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_cron/?output_format=md#description)󠁿

The [spawn_cron()](https://developer.wordpress.org/reference/functions/spawn_cron/)
function attempts to make a non-blocking loopback request to `wp-cron.php` (when
alternative cron is not being used). However, the [wp_remote_post()](https://developer.wordpress.org/reference/functions/wp_remote_post/)
function does not always respect the `timeout` and `blocking` parameters. A timeout
of `0.01` may end up taking 1 second. When this runs at the [‘wp_loaded’](https://developer.wordpress.org/reference/hooks/wp_loaded/)
action, it increases the Time To First Byte (TTFB) since the HTML cannot be sent
while waiting for the cron request to initiate. Moving the spawning of cron to the
[‘shutdown’](https://developer.wordpress.org/reference/hooks/shutdown/) hook allows
for the server to flush the HTML document to the browser while waiting for the request.

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

    ```php
    function wp_cron(): void {
    	if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) {
    		if ( did_action( 'wp_loaded' ) ) {
    			_wp_cron();
    		} else {
    			add_action( 'wp_loaded', '_wp_cron', 20 );
    		}
    	} elseif ( doing_action( 'shutdown' ) ) {
    		_wp_cron();
    	} else {
    		add_action( 'shutdown', '_wp_cron' );
    	}
    }
    ```

[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#L990)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/cron.php#L990-L1002)

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

| Uses | 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.

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

Retrieves the number of times an action has been fired during the current request.

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

Returns whether or not an action hook is currently being processed.

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

Adds a callback function to an action hook.

  |

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

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

| Version | Description | 
| [6.9.0](https://developer.wordpress.org/reference/since/6.9.0/) | The [_wp_cron()](https://developer.wordpress.org/reference/functions/_wp_cron/) callback is moved from ['wp_loaded'](https://developer.wordpress.org/reference/hooks/wp_loaded/) to the ['shutdown'](https://developer.wordpress.org/reference/hooks/shutdown/) action, unless `ALTERNATE_WP_CRON` is enabled; the function now always returns void. | 
| [5.7.0](https://developer.wordpress.org/reference/since/5.7.0/) | Functionality moved to [_wp_cron()](https://developer.wordpress.org/reference/functions/_wp_cron/) to which this becomes a wrapper. | 
| [5.1.0](https://developer.wordpress.org/reference/since/5.1.0/) | Return value added to indicate success or failure. | 
| [2.1.0](https://developer.wordpress.org/reference/since/2.1.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_cron/?output_format=md#user-contributed-notes)󠁿

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/functions/wp_cron/?output_format=md#comment-content-643)
 2.   [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/wp_cron/#comment-643)
 3. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_cron%2F%23comment-643)
    Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_cron%2F%23comment-643)
 4. **Example**
     You should not call `wp_cron()` yourself, but it allows you to create
    scheduled events like this.
 5.     ```php
        if ( ! wp_next_scheduled( 'wpdocs_task_hook' ) ) {
        	wp_schedule_event( time(), 'hourly', 'wpdocs_task_hook' );
        }
        add_action( 'wpdocs_task_hook', 'wpdocs_task_function' ); // 'wpdocs_task_hook` is registered when the event is scheduled
    
        /**
         * Send an alert by email.
         */
        function wpdocs_task_function() {
        	wp_mail( 'your@email.com', 'Automatic email', 'Automatic scheduled email from WordPress.');
        }
        ```
    
 6.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_cron%2F%3Freplytocom%3D643%23feedback-editor-643)

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