How to make a Custom Plugin Scheduler or CronJob in Wordpress

Posted By : Abhishek Bhatnagar | 03-May-2022

Sometimes you would have come up with a problem where you want to perform a Task/Function under some specific time Event.

Cronjob or Scheduler is a perfect solution where you can execute a Functionality easily in the background without Affecting the frontend.

 

Step 1 : First We will make a new Plugin file as 'file-name.php' in the plugins folder and add the necessary plugin template code in the header of that file so we can find it in the admin.

 

<?php
/*
Plugin Name:  Plugin Name
Description:  Plugin Description
Version:      1.0
Author:       Author name
*/
?>

 

Step 2 : Now we will make a function and hook it with a callback value.

              This hook will help us to apply filters for our scheduler and to check if that cron event is scheduled or not. In order to get your task to run you must create your own custom hook and give that hook the name of a function to execute

 

For Example :

add_action('node_count_once_ina_day', 'once_ina_day_event_func');


function once_ina_day_event_func()
{

      // code block which need to be executed on cron event

}

 

Here, 'once_ina_day_event_func' is our function which is hooked by  'node_count_once_ina_day' .

 

Step 3 : Scheduling the task by applying filter.

              Filter action is used to modify various types of internal data at runtime.

 

add_filter('cron_schedules', 'node_count_once_ina_day');
function node_count_once_ina_day($schedules)
{
    $schedules['once_ina_day'] = array(
        'interval'  =>86400,
        'display'   => __('Once in a day')
    );
    return $schedules;
}

 

Here, we are applying filter on our 'node_count_once_ina_day' hook by passsing a $schedules parameter.

This $scheduler param consist an array which holds our interval and display value.

For eg : I want my Cron Event to hit in every 24 hrs so i place the interval value as 86400 in seconds and display value as 'Once in a day' .

 

Step 4 : Checking if our scheduler task is not already scheduled. So we wrap the scheduling code in a check like this:

 

if (!wp_next_scheduled('node_count_once_ina_day')) {
    wp_schedule_event(time(), 'once_ina_day', 'node_count_once_ina_day');
}

Here, we will check if our sceduler is scheduled or not. If it is not then our wp_schedule_event will be executed.

wp_schedule_event  consists these 3 parameters are as follows:

  1. $timestamp – The UNIX timestamp of the first time this task should execute
  2. $recurrence – The name of the interval in which the task will recur in seconds ('once_ina_day')
  3. $hook – The name of our custom hook to call ('node_count_once_ina_day'

 

Now you just need to save the file and go to the wp-admin and activate the plugin from there.

 

Voila !!!  Your First Custom Plugin for Cron scheduled event is ready.  

 

 

 

About Author

Author Image
Abhishek Bhatnagar

Abhishek is a passionate developer always ready to learn new things and Technologies. He has good knowledge of HTML, CSS, PHP, Javascript and MySQL.

Request for Proposal

Name is required

Comment is required

Sending message..