Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to add custom intervals for the WP cron in a plugin. It run with "hourly", "daily" default recurrence time. It dont' run when I add my own custom recurrence time for the function wp_schedule_event.

I want to execute a function every X hours (defined by users).

Thanks.

Here is the code:

<?php
/*
Plugin Name: Test Cron
Description: Plugin to Test Cron Jobs.
Version: 1.00
*/
class WP_TestCron {
    function __construct()
    {
        register_activation_hook(__FILE__, array($this, 'TestCron_cron_activate'));
        register_deactivation_hook(__FILE__, array($this, 'TestCron_cron_deactivate'));
        add_filter('cron_schedules', array($this, 'TestCron_cron_update_schedules'));
        add_action('TestCron_cron_eventOne', array($this, 'TestCron_cron_fct'));
        add_action('TestCron_cron_eventTwo', array($this, 'TestCron_cron_fct'));
    }

    function TestCron_cron_activate() 
    {
        // Run => In Schedule Cronjobs 
        if (!wp_next_scheduled('TestCron_cron_eventOne')) { wp_schedule_event(time(), 'hourly', 'TestCron_cron_eventOne'); }

        // Dont't Run => Not in Schedule Cronjobs 
        if (!wp_next_scheduled('TestCron_cron_eventTwo')) { wp_schedule_event(time(), 'in_per_minute', 'TestCron_cron_eventTwo'); }
    }

    function TestCron_cron_deactivate() 
    {
        wp_clear_scheduled_hook('TestCron_cron_eventOne');
        wp_clear_scheduled_hook('TestCron_cron_eventTwo');
    }

    function TestCron_cron_update_schedules() 
    {
        return array(
            'fively' => array('interval' => 300, 'display' => 'Once in Five minutes'),
            'in_per_minute' => array('interval' => 60, 'display' => 'In every minute'),
            'in_per_ten_minute' => array('interval' => 60 * 10, 'display' => 'Once in Ten minutes'),
            'three_hourly' => array('interval' => 60 * 60 * 3,  'display' => 'Once in three hours')
        );  
    }

    function TestCron_cron_fct() 
    {
        $testCcron_post = array();
        $testCcron_post['post_title'] = 'Cron Update - '.date('d/m/y \a\t h:i A');
        $testCcron_post['post_content'] = 'WP-Cron job is working!';
        $testCcron_post['post_status'] = 'publish';
        $testCcron_post['post_author'] = 1;
        $testCcron_post['post_category'] = array(1);

        wp_insert_post( $testCcron_post );  
    }   
}

if (is_admin()) $wpTC = new WP_TestCron();

?>
share|improve this question

2 Answers 2

WP-Cron doesn't run in the admin scope - remove your if ( is_admin() ) condition.

share|improve this answer
    
Found this to be true for me too. –  atwellpub Jan 3 at 1:32

I would also suggest not overwriting existing schedules.

The proper form would be to do:

function filter_cron_schedules( $schedules ) {
$schedules['fively'] = array(
    'interval' => 300,
    'display' => __('Once in Five minutes')
);  

$schedules['in_per_minute'] = array(
    'interval' => 60,
    'display' => __('In every minute')
);  

$schedules['in_per_ten_minute'] = array(
    'interval' => 600,
    'display' => __('Once in Ten minutes')
);  

$schedules['three_hourly'] = array(
    'interval' => 10800,
    'display' => __('Once in three hours')
);  
return $schedules;
}

Which would take the existing $schedules, add the 4 new intervals to it, and then return the updated array.

Mind you, I have had some serious problems with cron in class-based plugins before, a recent issue led me to this question.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.