How To Make Custom Cron In WordPress

In this article we will see how to make or execute custom cron using wordpress code without any plugin.

Now a days most of the things are performing automatically.

For example, when ever on any website they asked for the registration once you register your self and you will get emails repeatedly.

second one if you want to make api call to import or export any data on certain time limit. let’s dat once on a day, once in a month etc.

For that you don’t need do any manual execution of your code. Every time you are not able to run that code manually so to make that automation we can create cron scheduler in wordpress which will execute your code/function on the scheduled time.

add_filter( 'cron_schedules', 'your_function_name' );

There is a filter called “cron_schedules” will make the cron scheduler.

Here is the example code to make the cron.

add_filter( 'cron_schedules', 'test_cron_func' );
function test_cron_func( $schedules ) {
    $schedules['every_five_minutes'] = array(
            'interval'  => 5*60,
            'display'   => __( 'Every Five Minutes', 'domain' )
    return $schedules;

// Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'test_cron_func' ) ) {
    wp_schedule_event( time(), 'every_five_minutes', 'test_cron_func' );

// Hook into that action that'll fire every Five minutes
add_action( 'test_cron_func', 'cron_schedule_function' );

function cron_schedule_function() {
  echo "Test";

Here I have made cron for every five minutes.

add_action( 'test_cron_func', 'cron_schedule_function' );

In this action there is 2 parameter is there.

  1. Cron Function
  2. The function which needs to execute periodically

So in at the second position you need to pass your function name which you want to execute automatically based on your requirement.

If you want to execute multiple function within the same cron scheduler that is also possible. 

Here is the code for it.

// Hook into that action that'll fire every five minutes
add_action( 'test_cron_func', function () {
    // periodical run using cron
} );


Submit a Comment

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


Select Categories