How to run cron and execute function inside plugin in WordPress 3


Running cronjobs in WordPress is easy but seem there are lack of documentation that talking about this. Especially, if we want to running our custom plugin in WordPress from our cronjobs. Actually, there are two methods of running cron in WordPress. First, it’s running from loaded pages. Seconds, it’s running through cron UNIX servers (wget, lynx, or somethings).

For instance, I have plugin called “montage”, so I will defined scheduler event here :

1
2
3
4
5
6
7
8
9
10
11
/* Create Wordpress Schedule */
function montage_create_schedule() {
    if(! wp_next_scheduled(‘montage_execute_post’)) {
        wp_schedule_event(time(), ‘hourly’ , ‘montage_execute_post’);
    }  
   
//  wp_clear_scheduled_hook(‘montage_execute_post’);
//  do_action(‘montage_execute_post’);
}

add_action( ‘init’, ‘montage_create_schedule’ );

What this is mean? I create montage scheduler, that will schedule for running “montage_execute_post”. This schedule for every hour (hourly). I put two commented line for debug. So, if your schedule event doesn’t work, you can test it by un-comment this two lines.

Now, i will create “montage_execute_post” :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
add_action(‘montage_execute_post’, ‘montage_builder’);

/**
 * This function will execute when cronjobs schedule works
 */
function montage_builder() {
    global $wpdb;
   
    $failed_posts = $wpdb->get_results(
            "SELECT ID
            FROM $wpdb->posts
            WHERE post_content = ‘no content here’
            "
    );

    foreach($failed_posts as $delete_post) {
        wp_delete_post($delete_post->ID, TRUE);
    }  
}

Now, for every hour, “montage_builder()” executed!

How about making custom cron time ?
It’s easy, first you should define your custom time and attached into “wp_schedule_event”.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* Create custom cron interval */
add_filter( ‘cron_schedules’, ‘montage_cron_schedules’);
function montage_cron_schedules(){
    return array(
            ‘1_minute’ => array(
                    ‘interval’ => 60,
                    ‘display’ => ‘In every Mintue’
            ),
            ’10_minute’ => array(
                    ‘interval’ => 60 * 10,
                    ‘display’ => ‘In every two Mintues’
            ),
            ‘3_hourly’ => array(
                    ‘interval’ => 60 * 60 * 3,
                    ‘display’ => ‘Once in Three minute’
            )
    );
}

Then you just update your scheduler. For instance, I use “1_minute” :

1
2
3
4
5
6
7
8
9
10
/* Create Wordpress Schedule */
function montage_create_schedule() {
    if(! wp_next_scheduled(‘montage_execute_post’)) {
        wp_schedule_event(time(), ‘1_minute’ , ‘montage_execute_post’);
    }  
   
//  wp_clear_scheduled_hook(‘montage_execute_post’);
//  do_action(‘montage_execute_post’);
}
add_action( ‘init’, ‘montage_create_schedule’ );

Easy right?

How if I run cronjobs from servers without cron taken for every load pages ?
First, you should edit “wp-config.php” and insert this after “DB_COLLATE” defined :

1
define(‘DISABLE_WP_CRON’, true);

Then you can pointing your servers UNIX cron to “wp-cron.php?doing_wp_cron”, eg:

1
*/30 * * * * wget http://wpscale.com/wp-cron.php?doing_wp_cron > /dev/null 2>&1

So, now you can run your plugin through default WordPress cronjobs or UNIX.


Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.