In the previous part of this series, we saw how to create schedule a cron, create a custom cron and unschedule it. If you missed it, I highly recommend reading that first as these two tutorials are connected. That was Using Cron Job in WordPress – Schedule Events With WP-Cron: Part 1
In this part of this Using Cron Job in WordPress series, we will see how to create an options page to show all cron jobs available in WordPress, all scheduled cron events and how to schedule a single event (instead of a recurring cron job).
Show all Cron events available in WordPress:
/** * Create an options page */ add_action('admin_menu', function() { add_options_page('WP Cron Settings', 'WP Cron Settings', 'manage_options', 'wptutshub-cron', function() { $cron = _get_cron_array(); ?> <div class="wrap"> <h2>All Crons</h2> <?php foreach ($cron as $time => $hook) { echo "<h3>$time</h3>"; print_r($hook); } ?> </div> <?php }); });
Explanations:
Here I have registered an options page using add_options_page function which takes a good number of parameters. You can see more about this function at WordPress Codex (add_options_page). To get an array of all available cron events in WordPress, we have used a function named _get_cron_array. Anyway, this function is only for private use or only for our understanding.
Show all Scheduled Cron Events:
Now, we will enlist all of the scheduled cron events.
/** * Create an options page */ add_action('admin_menu', function() { add_options_page('WP Cron Settings', 'WP Cron Settings', 'manage_options', 'wptutshub-cron', function() { $schedules = wp_get_schedules(); ?> <div class="wrap"> <h2>Scheduled Cron Events</h2> <pre><?php print_r($schedules); ?></pre> </div> <?php }); });
Explanations:
In this part, we have removed the previous _get_cron_array function and added a new function named wp_get_schedules which will return all scheduled cron jobs or events.
If we now want to make it a little bit readable by showing display name and interval aside, it will look like below:
/** * Create an options page */ add_action('admin_menu', function() { add_options_page('WP Cron Settings', 'WP Cron Settings', 'manage_options', 'wptutshub-cron', function() { $schedules = wp_get_schedules(); ?> <div class="wrap"> <h2>Scheduled Cron Events</h2> <?php foreach ($schedules as $name) { echo "<h3>". $name['display']. ': '. $name['interval']. "</h3>"; } ?> </div> <?php }); });
Scheduling a Single Cron (instead of a recurring cron job):
You may find the necessity of scheduling a single event which will run only once instead of running repeatedly or recurrently, unlike a conventional cron job (like sending an email in 1 hour or make a query to a specific API). Well, in that case, you need to use wp_schedule_single_event() function which accepts three parameters (timestamps, hook and options arguments to pass to the hook functions.
Example:
wp_schedule_single_event( time() + 3600, 'wptutshub_cron_hook', array() );
I hope this article series has helped you to get started with WP-Cron event/job scheduling and how to use it in your plugin or themes. If you have any questions at all, feel free to comment below.
Leave a Reply