# Protect wp-cron.php From Outside Requests

> Requires a secret key for requests to wp-cron.php and adds that key automatically to WordPress's own cron calls, so outsiders cannot hammer the endpoint to slow your site down.

- Type: PHP
- Runs: Run everywhere
- Category: [Security](https://wpide.com/library/category/security/)
- Tags: cron, hardening, dos
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/sec-protect-wp-cron-endpoint/

## Code

```php
$wpide_cron_key = function () {
    return substr( wp_hash( 'wpide-cron-key' ), 0, 20 );
};

add_filter( 'cron_request', function ( $request ) use ( $wpide_cron_key ) {
    $request['url'] = add_query_arg( 'wpide_cron_key', $wpide_cron_key(), $request['url'] );
    return $request;
} );

add_action( 'init', function () use ( $wpide_cron_key ) {
    if ( ! wp_doing_cron() || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
        return;
    }

    $script = isset( $_SERVER['SCRIPT_NAME'] ) ? basename( sanitize_text_field( wp_unslash( $_SERVER['SCRIPT_NAME'] ) ) ) : '';
    $key    = isset( $_GET['wpide_cron_key'] ) ? sanitize_text_field( wp_unslash( $_GET['wpide_cron_key'] ) ) : '';

    if ( 'wp-cron.php' === $script && ! hash_equals( $wpide_cron_key(), $key ) ) {
        status_header( 403 );
        exit;
    }
}, 0 );

add_filter( 'debug_information', function ( $info ) use ( $wpide_cron_key ) {
    $info['wpide-cron'] = array(
        'label'  => 'WPIDE cron key',
        'fields' => array(
            'url' => array( 'label' => 'Cron URL for server cron jobs', 'value' => add_query_arg( 'wpide_cron_key', $wpide_cron_key(), site_url( 'wp-cron.php' ) ), 'private' => true ),
        ),
    );

    return $info;
} );
```

## Notes

If a server cron job calls wp-cron.php, add ?wpide_cron_key=YOUR_KEY to its URL; the key is shown under Tools > Site Health > Info > WPIDE cron key.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Protect wp-cron.php From Outside Requests" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
