场景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在自定义时间间隔下成功运行。
编辑:
除默认内置间隔外,两种方案都会以自定义间隔时间失败。