将wp_cron与自定义时间间隔和注册激活挂钩一起使用

时间:2013-06-14 作者:Adam

场景1:添加cron任务失败wp_schedule_event 使用custom interval 插件激活时使用register_activation_hook 由于尚未识别自定义间隔,类定义外部无法工作;

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\' );
    }

}
场景2:添加cron任务失败wp_schedule_event 使用custom interval 插件激活时使用register_activation_hook 由于调用add_filter(\'cron_schedules\', ...); 也会在事件链中激发。

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\' );
    }

} 
问题

如何让场景1或场景2在自定义时间间隔下成功运行。

编辑:

除默认内置间隔外,两种方案都会以自定义间隔时间失败。

1 个回复
最合适的回答,由SO网友:kaiser 整理而成

因此,正如我们在聊天中所讨论的,有一件事(有线的):

钩子名称不能(!)包含下划线。

为了让自己站在拯救生命的一边,最好把它降到最低。

结束

相关推荐

将USER_ACTIVATION_KEY用于其他目的

我需要知道使用是否安全user_activation_key (来自WP_User) 用于其他目的,如电子邮件验证(我将创建的一个功能,在激活创建的帐户之前先发送电子邮件验证)?下面是工作原理。用户将使用我的自定义注册表进行注册,该注册表仅在前端可用。成功注册后,将通过电子邮件通知用户,已使用以下约定沿注册时创建的激活链接创建了帐户:http://www.example.com/verify/?key=SAMPLEACTIVATIONKEY4321你认为这样安全吗?