当我在主插件文件(plugin.php)的顶部安排一个事件时,cron会被添加到wp_options cron选项中.
wp_schedule_event( time() + 10, 'hourly', 'this_is_my_action' );
这很好,它添加了新的cron.但是,当我尝试在插件类中的激活函数中使用相同的函数时,它不起作用.
在plugin.php里面我有:
$plugin = new My_Plugin(__FILE__);
$plugin->initialize();
Run Code Online (Sandbox Code Playgroud)
在My_Plugin类里面,我有:
class My_Plugin{
function __construct($plugin_file){
$this->plugin_file = $plugin_file;
}
function initialize(){
register_activation_hook( $this->plugin_file, array( $this, 'register_activation_hook' ) );
}
function register_activation_hook()
{
$this->log( 'Scheduling action.' );
wp_schedule_event( time() + 10, 'hourly', 'this_is_my_action' );
}
function log($message){
/*...*/
}
}
Run Code Online (Sandbox Code Playgroud)
当我激活插件时,日志会被写入,但是cron没有被添加到wordpress数据库中.有什么想法吗?
小智 -2
尝试这个:
class My_Plugin{
function __construct($plugin_file){
$this->plugin_file = $plugin_file;
}
function initialize(){
register_activation_hook( $this->plugin_file, array( $this, 'register_activation_hook' ) );
}
function register_activation_hook()
{
$this->log( 'Scheduling action.' );
wp_schedule_event( time() + 10, 'hourly', array( $this,'this_is_my_action' ));
}
function this_is_my_action(){
//do
}
function log($message){
}
}
Run Code Online (Sandbox Code Playgroud)
您需要添加array($this,'name_function')到日程表中。