Using wp_cron with custom time interval and the register activation hook

Scenario 1: fails

Adding a cron task wp_schedule_event with a custom interval on plugin activation using register_activation_hook outside the class definition fails to work because the custom interval is not recognized yet;

register_activation_hook(__FILE__, array('Test', 'test_plugin_activated' ) );
register_deactivation_hook(__FILE__, array('Test', 'test_plugin_deactivated' ) );  
add_action('plugins_loaded', array ( Test::get_instance(), 'plugin_setup' ) );

 class Test 
 {

    protected static $instance = NULL;

    public static function get_instance()
    {

        if ( null === self::$instance )
        {
            self::$instance = new self;
        }
        return self::$instance; 

    }

    public function plugin_setup()
    {
        //init stuff...        
    }

    public function __construct() 
    {
        add_filter( 'cron_schedules', array($this, 'custom_cron_schedule') );
    }

    public function custom_cron_schedule( $schedules ) 
    {
       $schedules['minute'] = array(
        'interval' => 60, 
        'display' => __( 'Once per minute' )
       );
       return $schedules;
    } 

    public static function test_plugin_activated() 
    {
        wp_schedule_event( time(), 'minute', 'MINUTE_EVENT') ;
    }

    public static function test_plugin_deactivated() 
    {
        wp_clear_scheduled_hook( 'MINUTE_EVENT' );
    }

}

Scenario 2: fails

Adding a cron task with wp_schedule_event with a custom interval on plugin activation using register_activation_hook inside the class constructor does work because the call to the add_filter('cron_schedules', ...); is also fired in the chain of events.

Read More
add_action('plugins_loaded', array ( Test::get_instance(), 'plugin_setup' ) );

 class Test 
 {

    protected static $instance = NULL;

    public static function get_instance()
    {

        if ( null === self::$instance )
        {
            self::$instance = new self;
        }
        return self::$instance; 

    }

    public function plugin_setup()
    {
        //init stuff...        
    }

    public function __construct() 
    {
        register_activation_hook(__FILE__, array($this, 'test_plugin_activated' ) );
        register_deactivation_hook(__FILE__, array($this, 'test_plugin_deactivated' ) ); 
        add_filter( 'cron_schedules', array($this, 'custom_cron_schedule') );
    }

    public function custom_cron_schedule( $schedules ) 
    {
       $schedules['minute'] = array(
        'interval' => 60, 
        'display' => __( 'Once per minute' )
       );
       return $schedules;
    } 

    public function test_plugin_activated() 
    {
        wp_schedule_event( time(), 'minute', 'MINUTE_EVENT') ;
    }

    public function test_plugin_deactivated() 
    {
        wp_clear_scheduled_hook( 'MINUTE_EVENT' );
    }

} 

Question

How can I get scenario 1 or 2 to work successfully with a custom time interval.

Edit:

Both scenarios fail with a custom interval time, other than the default inbuilt intervals.

Related posts

1 comment

  1. So as we discussed this in chat, there was one (wired) thing:

    The hook name must not(!) contain underscores.

    To keep yourself on the save side of life, better lower case it as well.

Comments are closed.