下面是我的插件。将安装到wp-content
<?php // If this file is called directly, abort.
namespace Booker;
if ( ! defined( \'WPINC\' ) ) {
die;
}
/**
* Currently plugin version.
* Start at version 1.0.0 and use SemVer - https://semver.org
* Rename this for your plugin and update it as you release new versions.
*/
define( \'PLUGIN_NAME_VERSION\', \'1.0.0\' );
/**
* The code that runs during plugin activation.
* This action is documented in includes/class-booker-activator.php
*/
function activate_booker() {
require_once plugin_dir_path(__FILE__) . \'includes/class-booker-activator.php\';
error_log("Running Activation");
Booker_Activator::activate();
}
/**
* The code that runs during plugin deactivation.
* This action is documented in includes/class-booker-deactivator.php
*/
function deactivate_booker() {
require_once plugin_dir_path(__FILE__) . \'includes/class-booker-deactivator.php\';
error_log("Running Deactivation");
Booker_Deactivator::deactivate();
}
register_activation_hook( __FILE__, \'activate_booker\');
register_deactivation_hook( __FILE__, \'deactivate_booker\');
?>
它当然不会执行,但插件会按预期停用和激活。目前,这些函数中定义的函数调用中没有代码。。。。但缺少日志消息表明没有执行。然而,我遇到了一个问题:
该插件在激活期间生成了2368个字符的意外输出。如果您注意到“headers ready sent”消息、联合提要问题或其他问题,请尝试停用或删除此插件。
在我的日志中,我得到:
[08-Oct-2019 23:51:20 UTC] PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, function \'activate_booker\' not found or invalid function name in G:\\wamp64\\www\\wordpress\\wp-includes\\class-wp-hook.php on line 286
[08-Oct-2019 23:51:20 UTC] PHP Stack trace:
[08-Oct-2019 23:51:20 UTC] PHP 1. {main}() G:\\wamp64\\www\\wordpress\\wp-admin\\plugins.php:0
[08-Oct-2019 23:51:20 UTC] PHP 2. activate_plugin() G:\\wamp64\\www\\wordpress\\wp-admin\\plugins.php:44
[08-Oct-2019 23:51:20 UTC] PHP 3. do_action() G:\\wamp64\\www\\wordpress\\wp-admin\\includes\\plugin.php:672
[08-Oct-2019 23:51:20 UTC] PHP 4. WP_Hook->do_action() G:\\wamp64\\www\\wordpress\\wp-includes\\plugin.php:465
[08-Oct-2019 23:51:20 UTC] PHP 5. WP_Hook->apply_filters() G:\\wamp64\\www\\wordpress\\wp-includes\\class-wp-hook.php:310
还有
[09-Oct-2019 00:44:43 UTC] PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, function \'deactivate_booker\' not found or invalid function name in G:\\wamp64\\www\\wordpress\\wp-includes\\class-wp-hook.php on line 286
[09-Oct-2019 00:44:43 UTC] PHP Stack trace:
[09-Oct-2019 00:44:43 UTC] PHP 1. {main}() G:\\wamp64\\www\\wordpress\\wp-admin\\plugins.php:0
[09-Oct-2019 00:44:43 UTC] PHP 2. deactivate_plugins($plugins = *uninitialized*, $silent = *uninitialized*, $network_wide = *uninitialized*) G:\\wamp64\\www\\wordpress\\wp-admin\\plugins.php:192
[09-Oct-2019 00:44:43 UTC] PHP 3. do_action($tag = *uninitialized*, $arg = *uninitialized*) G:\\wamp64\\www\\wordpress\\wp-admin\\includes\\plugin.php:792
[09-Oct-2019 00:44:43 UTC] PHP 4. WP_Hook->do_action($args = *uninitialized*) G:\\wamp64\\www\\wordpress\\wp-includes\\plugin.php:465
[09-Oct-2019 00:44:43 UTC] PHP 5. WP_Hook->apply_filters($value = *uninitialized*, $args = *uninitialized*) G:\\wamp64\\www\\wordpress\\wp-includes\\class-wp-hook.php:310
更改函数名称时出现其他错误:
[09-Oct-2019 02:08:03 UTC] PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, function \'booker_deactivate\' not found or invalid function name in G:\\wamp64\\www\\wordpress\\wp-includes\\class-wp-hook.php on line 286
[09-Oct-2019 02:08:03 UTC] PHP Stack trace:
[09-Oct-2019 02:08:03 UTC] PHP 1. {main}() G:\\wamp64\\www\\wordpress\\wp-admin\\plugins.php:0
[09-Oct-2019 02:08:03 UTC] PHP 2. deactivate_plugins() G:\\wamp64\\www\\wordpress\\wp-admin\\plugins.php:192
[09-Oct-2019 02:08:03 UTC] PHP 3. do_action() G:\\wamp64\\www\\wordpress\\wp-admin\\includes\\plugin.php:792
[09-Oct-2019 02:08:03 UTC] PHP 4. WP_Hook->do_action() G:\\wamp64\\www\\wordpress\\wp-includes\\plugin.php:465
[09-Oct-2019 02:08:03 UTC] PHP 5. WP_Hook->apply_filters() G:\\wamp64\\www\\wordpress\\wp-includes\\class-wp-hook.php:310
最合适的回答,由SO网友:Sally CJ 整理而成
所以“activate\\u plugin”是问题中的一个输入错误。但即便如此,作为一个友好的提醒(对我们所有人来说),activate_plugin()
是WordPress中现有的函数。您应该在自定义函数中使用唯一前缀(在全局范围中定义):
function my_plugin_activate_plugin() {
...
}
register_activation_hook( __FILE__, \'my_plugin_activate_plugin\' );
问题及其解决方案
日志中的错误/警告
function \'activate_booker\' not found or invalid function name
发生这种情况的原因是您正在文件中使用命名空间,但未能正确引用命名空间中的回调函数:
// Incorrect - These are referencing to global functions.
register_activation_hook( __FILE__, \'activate_booker\' );
register_deactivation_hook( __FILE__, \'deactivate_booker\' );
// Correct syntax when referencing to namespace functions.
register_activation_hook( __FILE__, \'Booker\\activate_booker\' );
register_deactivation_hook( __FILE__, \'Booker\\deactivate_booker\' );
// Or use the __NAMESPACE__ constant.
register_activation_hook( __FILE__, __NAMESPACE__ . \'\\activate_booker\' );
register_deactivation_hook( __FILE__, __NAMESPACE__ . \'\\deactivate_booker\' );
但请注意,除非遇到致命错误,否则插件仍将被激活/停用;只是不会调用激活/停用回调。
这里有一个好消息article 它讨论了在WordPress中使用PHP名称空间。