# Run a Daily Task With WP-Cron

> Schedules a custom task once a day and gives you a function to fill in, for example to clean up data or send a report. Includes the scheduling and the hook.

- Type: PHP
- Runs: Run everywhere
- Category: [Developer](https://wpide.com/library/category/developer/)
- Tags: cron, scheduling, automation, tasks
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/dev-daily-cron-task/

## Code

```php
add_action( 'init', function () {
    if ( ! wp_next_scheduled( 'wpide_daily_task' ) ) {
        // First run tomorrow at 03:00 site time.
        $first = new DateTime( 'tomorrow 03:00', wp_timezone() );
        wp_schedule_event( $first->getTimestamp(), 'daily', 'wpide_daily_task' );
    }
} );

add_action( 'wpide_daily_task', function () {
    // Your daily code goes here. Example: log that the task ran.
    error_log( '[wpide_daily_task] ran at ' . wp_date( 'Y-m-d H:i:s' ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
} );
```

## Notes

Put your code inside the wpide_daily_task callback. To stop the task after removing the snippet, run "wp cron event delete wpide_daily_task".

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Run a Daily Task With WP-Cron" 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/
