Web Dev Tuts

/ Stuffs About Website Development /

Menu
  • Home
  • Wp Plugins
  • How To
  • FAQ
    • GIT
    • JQuery
    • PHP
    • WooCommerce
    • WordPress
  • Contact Us
Menu

How to Use WP Cron

Posted on January 15, 2018June 24, 2018 by Edesa Cee

Scheduling an event in WordPress is very common. This functionality is commonly known as CRON. Here are the steps on how to can create CRON tasks.

Step 1: Determine the desired recurrence

There are three predefined recurrences defined by WordPress, namely: daily, twicedaily, & hourly.

If you want to define your own recurrence, you can do so. Below is an example of adding a recurrence every2mins which obviously means that the recurrence every 2 minutes:

add_filter('cron_schedules',  'add_cron_recurrence');

function add_cron_recurrence($schedules) {
        if(!isset($schedules[$this->_name])){
            $schedules['every2mins'] = array(
                'interval' => 2 * 60, //120 seconds
                'display' => __('Run every 2 mins');
        }

        return $schedules;        
}

Change values as desired.

Step 2: Register your cron task(s)

Here is a simple way to implement a cron or registering an event which will be executed every hour:

register_activation_hook(__FILE__, 'my_activation');

function my_activation() {
    if (! wp_next_scheduled ('my_hourly_event')) {
        wp_schedule_event(time(), 'hourly', 'my_hourly_event');
    }
}  

add_action('my_hourly_event', 'do_this_hourly');

function do_this_hourly() {
    // do something every hour
}

register_deactivation_hook($file, 'my_deactivation');

function my_deactivation() {
	wp_clear_scheduled_hook('my_hourly_event');
}

To change the recurrence, run it every 2 minutes, for example, we can use the recurrence above. In the function my_activation, look for the ff. line:

wp_schedule_event(time(), ‘hourly’, ‘my_hourly_event’);

Change hourly to every2mins, or the name of the recurrence you defined.


You can create as many custom recurrence and events as needed. It’s just a matter of changing values. To make it easier for you, I created a class which will allow you to register events into the WordPress Cron without the need to do the above steps.

Step 1: Copy the Cron Class from WDT-Framework

Step 2: Include/Require the class in your plugin.

Step 3: On the main file of your plugin, add the ff. codes:

new \wdtf\WPCronEvent(__FILE__, 60, 'executeThisFunc');

60 is the interval of the recurrence (in seconds). You can also use ‘daily’, ‘twicedaily’, or ‘hourly’
executeThisFunc is the name of the function that will get executed and you can rename it.

Step 4: Define your function.


Don’t forget to reactivate your plugin to make the cron work.


 

Category: Wordpress

Leave a Reply Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to add wp_editor Dynamically
  • How to Hide Certain Pages from Certain Users (WP Dashboard)
  • How to Set Up Gmail SMTP
  • How to Set Up Virtual Host in Localhost & Xampp
  • How to Determine the Current WordPress Version Used
  • AngularJs
  • CSS
  • For Me Only
  • geo location api
  • GIT
  • GoDaddy
  • Hosting
  • How To
  • Javascript
  • JQuery
  • Magento
  • My Notes
  • PHP
  • Private
  • Sass
  • WooCommerce
  • Wordpress
  • Xampp
© 2023 Web Dev Tuts | Powered by Minimalist Blog WordPress Theme