Run a wp_schedule_event Recurrence Every 3 Minutes

Do you want to run a WordPress Cron job every few minutes? By default, wp_schedule_event lets you choose a recurrence interval of ‘hourly’, ‘twicedaily’, or ‘daily’ for WP Cron jobs. Here, I show you how to add a custom recurrence interval of 3 minutes.

You can change 3 minutes to 5, 10, or 15 minutes, as desired. You can also add multiple recurrence intervals. For example, one for 3 minutes, and one for 15 minutes. I’ll show you how to do that, below. I’ll also show you how to run a test event so that you can make sure that your cron job works.

You may want to run a WP Cron job every 3 minutes, or so, for testing purposes. For example, you may be setting a cron job for a later interval, but you just want to test it now.

To add an interval of 3 minutes to the WP Cron schedules, use this:

Code block 1

function isa_add_cron_recurrence_interval( $schedules ) {

	$schedules['every_three_minutes'] = array(
			'interval' 	=> 180,
			'display' 	=> __( 'Every 3 Minutes', 'textdomain' )
	);
	
	return $schedules;
}
add_filter( 'cron_schedules', 'isa_add_cron_recurrence_interval' );

To change that interval to 15, 10, or 5 minutes, do these steps.

  • Change the 180 on line 4 to the number of seconds for your desired time. That is how you set the interval of time. To set the interval to 5 minutes, set the interval to 300. For 10 minutes, set the interval to 600. For 15 minutes, set it to 900.
  • Then, on line 3, change the key, every_three_minutes accordingly. If you use 5 minutes, change every_three_minutes to every_five_minutes.
  • Then, on line 5, change the label, ‘Every 3 Minutes’ accordingly. For example, for 5 minutes, change it to ‘Every 5 Minutes’.

You Can Add Multiple Recurrence Intervals

You can create as many intervals as you need. This example creates one interval of 3 minutes, and another one of 15 minutes.

function isa_add_cron_recurrence_interval( $schedules ) {

	$schedules['every_three_minutes'] = array(
			'interval' 	=> 180,
			'display' 	=> __( 'Every 3 Minutes', 'textdomain' )
	);

	$schedules['every_fifteen_minutes'] = array(
			'interval' 	=> 900,
			'display' 	=> __( 'Every 15 Minutes', 'textdomain' )
	);	
	
	return $schedules;
}
add_filter( 'cron_schedules', 'isa_add_cron_recurrence_interval' );

Example Usage: Use Your New Interval To Set Up a Scheduled Event

Now, you can use ‘every_three_minutes’ as the recurrence parameter when you use wp_schedule_event. (If you changed ‘every_three_minutes’ to something else, above, be sure to make that change here, as well.)

wp_schedule_event( time(), 'every_three_minutes', 'your_three_minute_action_hook' );

Always remember to check if an event is scheduled to prevent duplicate events.

Code block 2

if ( ! wp_next_scheduled( 'your_three_minute_action_hook' ) ) {
	wp_schedule_event( time(), 'every_three_minutes', 'your_three_minute_action_hook' );
}

Test That Your WP Cron Event Works

To check if your new 3 minute interval works, you can set up a test cron event. The following example sets up a WP cron job that will send you an email every three minutes.

First, you should have added Code block 1 and Code block 2 from above.

Next, add the following to create the test event that will send you an email in three minutes. You must replace you@gmail.com on line 4 with your own email address where you want to receive the email.

Code block 3

add_action('your_three_minute_action_hook', 'isa_test_cron_job_send_mail');

function isa_test_cron_job_send_mail() {
	$to = 'you@gmail.com';
	$subject = 'Test my 3-minute cron job';
	$message = 'If you received this message, it means that your 3-minute cron job has worked! :) ';

	wp_mail( $to, $subject, $message );

}

Important note: you may want to use the PHP mail() function instead of wp_mail() if you have disabled wp_mail(). I add this note because I use this in development environments in which I usually have disabled wp_mail(). If you want to make this change, then change line 8 to this:

mail( $to, $subject, $message, 'From: you@yoursite.com' . "rn" );

You must replace you@yoursite.com with your own email address. For this to work on a live site, most hosting companies require this “from” email address to be at your actual site. So, a Gmail or Yahoo mail will not work unless you set up an SMTP plugin.

What Next?

Visit your site to activate the WP Cron (this is how WP Cron works). After three minutes have passed, visit your site again. Then, check your email. If you received the test email, you’ll know that your new WP Cron recurrence interval works. Remove all the test code that you added (Code block 2 and Code block 3) from your site. Then, run this once to clear any future scheduled test emails:

wp_clear_scheduled_hook( 'your_three_minute_action_hook' );

Troubleshooting

If you didn’t receive the test email, visit your site again (refresh the page). Then, check your email. Make sure that the problem is not simply that you’re not receiving any WordPress emails. You can check to see if you receive regular WordPress emails like this: Go to your WP login page (/wp-login.php) and click “Lost your password?” Enter your email address and submit the form. If you receive an email from WordPress, then you know that wp_mail() works on your site. In this case, the code on this page should work on your site. Double check the steps.

See more: ,

We've 13 Responses

    • January 12th, 2017 at 4:22 pm

      This goes in a functions file. Either one of these:

      1. Your theme’s functions.php file (but it will be erased on theme updates).
      2. Your child theme’s functions.php file, if you’re using a child theme.
      3. You could make it into a plugin.

      See this for help.

      Isabel
      • January 12th, 2017 at 4:34 pm

        Hi Isabel, Thanks for ur quick response. I added the code in functions.php. But the cron job works only if I visit the site. If I close the browser, cron is not working for me. Is there anything I need to do? I am using wpengine.

        Thanks

        rifat
  1. August 7th, 2017 at 11:34 am

    Hey,

    it works perfectly. But how can i use multiple interval ?
    I would like to execute “Insert” SQL query every 60 secs and “Delete” this one after 59secs.

    But only one Cron works. I tried to send 2 different mail (KO for DELET and OK for INSERT) and i receive only KO.

    Any idea ?

    Mona
  2. October 11th, 2017 at 8:13 am

    Hi Isabel !

    I want to run the cron job once in a month (automatically on 2nd of every month at 2 AM).
    So how can I achieve this?

    Please help.

    Naval Kishore Kandpal
  3. September 21st, 2018 at 2:20 am

    How to set cron to run after 24 hrs?

    $schedules[‘every_three_minutes’] = array(
    ‘interval’ => 180,
    ‘display’ => __( ‘Every 3 Minutes’, ‘textdomain’ )
    );

    What should i place instead of every_three_minutes for 24 hrs ?

    Sanjay
    • May 19th, 2019 at 4:22 pm

      WordPress already has one set for ‘daily’ which will run every 24 hours. So you don’t need Code Block 1. Just use 'daily' as the 2nd parameter of wp_schedule_event().

      Isabel

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]