我正在尝试为我的插件注册一个激活挂钩。插件使用类,实际的激活方法保存在单独的管理类中。阅读文档register_activation_hook()
它似乎是第一个参数$file
应始终是主插件文件的文件:
wp-content/plugins目录中主插件文件的路径。完整路径将起作用。
这两个类被保存在不同的文件中,看起来像这样。
这是主插件文件:
<?php
/* my-plugin.php */
include_once(plugin_dir_path(__FILE__) . \'/admin/my-plugin-admin.php\');
register_activation_hook(__FILE__, array(\'MyPluginAdmin\', \'activate\'));
class MyPlugin {
}
这是管理部分,其中还包含激活例程:
<?php
/* my-plugin-admin.php */
class MyPluginAdmin {
public static function activate() {
// This method is called in both cases,
// but in the first example a php warning is generated
}
}
我的问题是静电
activate
方法实际上是在激活时触发的,但我仍然收到一条php警告,如下所示:
PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, class \'MyPluginAdmin\' not found in /var/www/html/wp/wp-includes/class-wp-hook.php on line 287
PHP Stack trace:
1. {main}() /var/www/html/wp/wp-admin/plugins.php:0
2. activate_plugin($plugin = \'my-plugin/my-plugin.php\', $redirect = \'http://localhost/wp/wp-admin/plugins.php?error=true&plugin=my-plugin%2Fmy-plugin.php\', $network_wide = FALSE, $silent = *uninitialized*) /var/www/html/wp/wp-admin/plugins.php:43
3. do_action($tag = \'activate_my-plugin/my-plugin.php\', $arg = FALSE) /var/www/html/wp/wp-admin/includes/plugin.php:586
4. WP_Hook->do_action($args = array (0 => FALSE)) /var/www/html/wp/wp-includes/plugin.php:453
5. WP_Hook->apply_filters($value = \'\', $args = array (0 => FALSE)) /var/www/html/wp/wp-includes/class-wp-hook.php:311
6. call_user_func_array:{/var/www/html/wp/wp-includes/class-wp-hook.php:287}(array (0 => \'MyPluginAdmin\', 1 => \'activate\'), array (0 => FALSE)) /var/www/html/wp/wp-includes/class-wp-hook.php:287
如果我把电话改成
register_activation_hook()
进入:
register_activation_hook(plugin_dir_path(__FILE__) . \'/admin/my-plugin-admin.php\', array(\'MyPluginAdmin\', \'activate\'));
然后警告信息消失了。所以,我的问题是什么是正确的使用方法
register_activation_hook()
为什么它在上述两种情况下都能工作,尽管它在第一种情况下会发出警告?