If you need to make changes to scheduled events within your WordPress site, WP-CLI is a great tool to have. In this article, we'll show you how to run, create, and delete events within the WordPress cron, using WP-CLI.
Displaying All Cron Events
To display a list of all cron events currently scheduled on your WordPress site, use the following command from within your site's root directory:
pagely-wp cron event list
Running All WordPress Cron Events
If you want to trigger all currently due cron events (events that are due but haven't been run yet), you can run them with the following command:
pagely-wp cron event run --due-now
Manually Running a Scheduled Cron Event
To run all WordPress scheduled events manually, regardless of if they are due or not, you can use the following WP-CLI command:
pagely-wp cron event run --all
Scheduling a New Cron Event
Additional WP-Cron events can also be scheduled using WP-CLI. New cron events can be scheduled for any action hook within your site.
Scheduling a New Event for the Next Cron Run
The following command will schedule a new cron event for the desired action hook. This event will be triggered on the next time that the cron is run.
Be sure to replace my_example_action with the name of the action hook that you want to execute.
pagely-wp cron event schedule my_example_action
Scheduling a New Event for a Specific Date/Time
Scheduling a WordPress cron event to run on a specific date and time is similar to the example in the previous section, but requires an additional parameter that includes the date and time that the event should run.
pagely-wp cron event schedule my_example_action '+1 hour'
As you can see from the example above, a new event will be scheduled to run my_example_action in 1 hour.
Since the time parameter allows for any string that can use converted to a timestamp with the PHP strtotime() function, the following examples would also be valid:
Example: Run the Event on a Specific Date
pagely-wp cron event schedule my_example_action '7 July 2019'
Example: Run the Event in 1 Week
pagely-wp cron event schedule my_example_action '+1 week'
Example: Run the Event Next Monday
pagely-wp cron event schedule my_example_action 'next Monday'
For more information on the available formats, see the official PHP documentation on supported date and time formats.
Scheduling a New Recurring Cron Event
To create a new WordPress cron event that will repeat after a designated interval, you'll simply pass another argument containing how often the event will be run:
pagely-wp cron event schedule my_example_action now daily
In this example, we're using a WP-CLI command to tell WordPress to run my_example_action now and to run again once per day.
The available intervals are based on any currently defined cron intervals. By default, WordPress contains the following interval names:
- hourly: Runs the event once per hour.
- twicedaily: Runs the event twice per day (every 12 hours).
- daily: Runs the event once per day (every 24 hours).
If you need additional cron intervals, you can add them using the cron_schedules filter within your plugin or theme.
Deleting a Scheduled Cron Event
Deleting a scheduled cron event with WP-CLI is quite straightforward. To delete an existing event, run the following command, replacing my_example_action with the name of your event action hook:
pagely-wp cron event delete my_example_action