具有:
插件名称。php
// If this file is called directly, abort.
if ( ! defined( \'ABSPATH\' ) ) {
die();
}
define(\'PLUGINNAME_PLUGIN_URL\', plugin_dir_url( __FILE__ ));
define(\'PLUGINNAME_PLUGIN_DIR\', plugin_dir_path(__FILE__));
require_once( plugin_dir_path( __FILE__ ) . \'class-pluginname.php\' );
// Register hooks that are fired when the plugin is activated, deactivated, and uninstalled, respectively.
register_activation_hook( __FILE__, array( \'PluginName\', \'activate\' ) );
register_deactivation_hook( __FILE__, array( \'PluginName\', \'deactivate\' ) );
PluginName::get_instance();
类插件名称。php
/**
* Initialize the plugin
*
* @since 1.0.0
*/
private function __construct()
{
#add_action(\'init\', array($this, \'widget_add_vars\'));
#add_action(\'params\', array($this, \'catch_widget_query\'));
add_action(\'dailyExport\', array($this, \'_generateJson\'));
if (!wp_next_scheduled(\'dailyExport\')) {
wp_schedule_event( time(), \'daily\', \'dailyExport\' );
}
}
/**
* Return an instance of this class.
*
* @since 1.0.0
*
* @return object A single instance of this class.
*/
public static function get_instance()
{
// If the single instance hasn\'t been set, set it now.
if ( null == self::$instance )
self::$instance = new self;
return self::$instance;
}
/**
* Fired when the plugin is activated.
*
* @since 1.0.0
*/
public static function activate()
{
self::_generateJson();
wp_schedule_event(time(), \'daily\', \'dailyExport\');
}
/**
* Fired when the plugin is deactivated.
*
* @since 1.0.0
*/
public static function deactivate()
{
wp_clear_scheduled_hook(\'dailyExport\');
}
public function dailyExport()
{
return self::_generateJson();
}
我想知道,你是否认为
static
这个
activate
和
deactivate
函数,如果您认为我正确地处理了cron事件,则可以安排每日事件。
我写了我的插件,基于这个模板https://github.com/tommcfarlin/WordPress-Plugin-Boilerplate
SO网友:Adam
我和wp_cron
上周在一个插件中,我们进行了一场斗争,不再是口头上的,但作为参考,这是我所做的;
1) -将计划的cron事件设置为register_activation_hook
2)-删除上的计划cron事件register_deactivation_hook
如果您担心计划的cron事件可能会从数据库中删除,那么您可以添加例程,因为您已经需要在下一个计划的时间戳中检查cron事件;
if (!wp_next_scheduled(\'dailyExport\')) {
//schedule event
wp_schedule_event( time(), \'daily\', \'dailyExport\' );
}
我的示例类;
register_activation_hook(__FILE__, array( \'Killa\', \'activated\' ) );
register_deactivation_hook( __FILE__, array( \'Killa\', \'deactivated\' ) );
add_action(\'plugins_loaded\', array ( Killa::get_instance(), \'plugin_setup\' ) );
class Killa
{
protected static $instance = NULL;
public static function get_instance()
{
if ( null === self::$instance )
{
self::$instance = new self;
self::load_files();
}
return self::$instance;
}
public function plugin_setup()
{
add_action(\'my_cron_event\', array($this, \'my_callback_function\'));
}
public function __construct()
{
//empty
}
public static function activated()
{
wp_schedule_event( time(), \'hourly\', \'my_cron_event\');
}
public static function deactivated()
{
wp_clear_scheduled_hook( \'my_cron_event\' );
}
public function my_callback_function()
{
//do your thing here... such as generate JSON
}
}
底线是,完全可以将寄存器激活/停用挂钩定义为静态函数,更恰当的做法是在单例类型模式下工作或
activated()/deactivated()
函数可能存在于控制器的另一个类调用中。
根据类结构的不同,在构造函数中也可以使用注册挂钩。