WordPress | Developing Custom scheduler in WordPress

| By Webner

Suppose you want to design a custom scheduler in wordpress plugin instead of using wordpress scheduler which has fixed values of recurrence which are ‘hourly’, ‘twicedaily’,’daily’. And if you want to make the scheduler dynamic instead of making it static i.e. instead of setting the fixed scheduler value in wordpress plugin code at activation time, one should be able to change the scheduler recurrence value externally according to situation demand.

For this purpose, first create a menu (here admin menu) for scheduler setting in the main plugin file as shown in the following manner:

function my_admin_menu() {
add_menu_page ( 'page_title', 'menu Title', 'manage_options', 'menu_slug', 'schedule_menu', 'icon_url',1);

Create a cron function to configure your own schedules. Here five new schedules are created of 10 minutes, 20 minutes, 3 hour, weekly, monthly:

function my_cron_schedules($schedules) {
if (! isset ( $schedules ["10min"] )) {
$schedules ["10min"] = array (
'interval' => 10 * 60,
'display' => __ ( 'Once every 10 minutes' )
if (! isset ( $schedules ["20min"] )) {
$schedules ["20min"] = array (
'interval' => 20 * 60,
'display' => __ ( 'Once every 20 minutes' ));
if (! isset ( $schedules ["3hr"] )) {
$schedules ["3hr"] = array (
'interval' => 3 * 60 * 60,
'display' => __ ( 'Once every 3 hours' ));
if (! isset ($schedules['weekly'])) {
$schedules["weekly"] = array(
'interval' => 604800,
'display' => __('Once Weekly'));
if (! isset ($schedules['monthly'])) {
$schedules["monthly"] = array(
'interval' => 2635200,
'display' => __('Once a month'));
return $schedules;
add_filter ( 'cron_schedules', 'my_cron_schedules' );

Now a function is written which includes the code for scheduling the event. Here ‘update_event’ is the hook attached to the scheduling function. Here the value is set to none, to not to start the scheduler on plugin activation:

function schedule_my_job() {
wp_schedule_event (time() ,'none', 'update_event' );

Following lines of code are added for attaching scheduler to the function hook. Here wp_get_schedule() returns true if event is scheduled. For the first time, on plugin activation this value will be false, so the following if condition will become true when function is not scheduled and ‘init’ action will be executed and scheduler function will be called. But since the recurrence value is set to none so scheduler will not start:

 if (! wp_get_schedule ( 'update_event' )) { 
add_action ( 'init', 'schedule_my_job', 10 );
add_action ( 'admin_menu', 'my_admin_menu' );
register_activation_hook ( __FILE__, 'init' );

Now to make the scheduler value controlled from the menu page:


Here in this menu page, the dropdown is designed which accepts the scheduler values externally. And when the form is submitted, new scheduler value will be set. This can be achieved by clearing the previous scheduler recurrence value and calling the WordPress scheduling function again with a new value:


function schedule_menu()
<form name="reschedule_form" method="post">
<span>Enter reschedule time:</span>
<select name="reschedule_time">
<option value="default" selected disabled>Select schedule time--</option>
<option value="20min">20 minutes</option>
<option value="hourly">1 hour</option>
<option value="3hr">3 hours</option>
<option value="twicedaily">Twice a day</option>
<option value="daily">daily</option>
<option value="weekly">Once in a week</option>
<option value="monthly">Once in a Month</option>
<option value="none">No schedule</option>
</select> <br /> <input type="submit" name="reschedule_submit" value="save">

if (isset($_POST['reschedule_submit'])) {
$reschedule_value = $_POST['reschedule_time'];
if (isset($reschedule_value) && $reschedule_value != 'default') {
$timestamp = wp_next_scheduled('update_csv_file');
wp_schedule_event(time(), $reschedule_value, 'update_event');

wp_clear_scheduled_hook(‘update_event’) clears the previous schedule associated with the schedule hook ‘update_event’ and wp_schedule_event() function schedules the hook with a new scheduling value.

Scheduler.php File

This is the php file containing the function which is to be scheduled at regular intervals:

// function code to execute...

Setting the scheduler start time- Scheduler can also be started at some specific time and time needs to be specified. Suppose if you want to set the scheduler to 5 minutes from the current time, then scheduler function can be modified as- wp_schedule_event (time()+5*60 ,$reschedule_value, ‘update_event’ );

Testing the next scheduling time by converting wordpress unix timestamp to Indian time zone – WordPress takes the schedule time argument in unix timestamp format for scheduler functions like wp_schedule_event() and wp_reschedule_event() and wordpress function wp_next_scheduled(‘hook_name’); returns the next schedule time in unix timestamp format. Unix time stamp can be easily converted to the Indian GST timezone with the following steps:

$timestamp = wp_next_scheduled('hook_name');
$dt = new DateTime('@'.$timestamp);
$dt->setTimeZone(new DateTimeZone('Asia/Kolkata'));
echo "
Next Scheduling Time:	".$dt->format('F j, Y, g:i a');

So one can easily read the time for the next scheduling event to occur:

To display the date time format in some other time zone just replace the zone value in function in function inside DateTimeZone(‘zone’). for eg $dt->setTimeZone(new DateTimeZone(‘America/Chicago’));

Leave a Reply

Your email address will not be published. Required fields are marked *