Wordpress/PHP - Namespaces/Autoloader - Fatal error: Uncaught TypeError: call_user_func_array(): Argument #1 ($function) must be a valid callback
Wordpress/PHP - Namespaces/Autoloader - Fatal error: Uncaught TypeError: call_user_func_array(): Argument #1 ($function) must be a valid callback
我正在完全从头开始创建我的第一个插件。每当我尝试将命名空间添加到 'template-plugin-activate.php' 文件时,我都会收到以下错误:
Plugin could not be activated because it triggered a fatal error.
Fatal error: Uncaught TypeError: call_user_func_array(): Argument #1 ($function) must be a valid callback, class "TemplatePluginActivate" not found in C:\xampp\htdocs\wordpress\wp-includes\class-wp-hook.php:292 Stack trace: #0 C:\xampp\htdocs\wordpress\wp-includes\class-wp-hook.php(316): WP_Hook->apply_filters('', Array) #1 C:\xampp\htdocs\wordpress\wp-includes\plugin.php(484): WP_Hook->do_action(Array) #2 C:\xampp\htdocs\wordpress\wp-admin\plugins.php(193): do_action('activate_templa...') #3 {main} thrown in C:\xampp\htdocs\wordpress\wp-includes\class-wp-hook.php on line 292
以下是我的文件。感谢任何帮助。
// inc/template-plugin.php
<?php
namespace Inc;
class TemplatePluginActivate
{
public static function activate() {
flush_rewrite_rules();
}
}
//模板-plugin.php
defined( 'ABSPATH' ) or die( 'You can\t access this file.' );
if ( file_exists( dirname( __FILE__ ) . '/vendor/autoload.php' ) ) {
require_once dirname( __FILE__ ) . '/vendor/autoload.php';
}
use Inc\TemplatePluginActivate;
if ( !class_exists( 'TemplatePlugin' ) ) {
class TemplatePlugin
{
//
}
}
require_once plugin_dir_path( __FILE__ ) . 'inc/template-plugin-activate.php';
register_activation_hook( __FILE__, array( 'TemplatePluginActivate', 'activate' ) );
require_once plugin_dir_path( __FILE__ ) . 'inc/template-plugin-deactivate.php';
register_activation_hook( __FILE__, array( 'TemplatePluginDeactivate', 'deactivate' ) );
// composer.json
{
"name": "author/template-plugin",
"description": "awesome starter plugin example",
"type": "project",
"license": "GPL",
"authors": [
{
"name": "author",
"email": ""
}
],
"minimum-stability": "dev",
"require": {},
"autoload": {
"psr-4": {"Inc\": "./inc"}
}
}
问题出在 register_activation_hook()
。
您的回调是 TemplatePluginActivate
class 的 activate()
方法。一旦您引入名称空间,就需要对其进行更新。 class 将是 Inc\TemplatePluginActivate
.
register_activation_hook( __FILE__, array( 'Inc\TemplatePluginActivate', 'activate' ) );
此外,您的停用操作也是 运行 激活。你需要的钩子是 register_deactivation_hook()
.
我正在完全从头开始创建我的第一个插件。每当我尝试将命名空间添加到 'template-plugin-activate.php' 文件时,我都会收到以下错误:
Plugin could not be activated because it triggered a fatal error.
Fatal error: Uncaught TypeError: call_user_func_array(): Argument #1 ($function) must be a valid callback, class "TemplatePluginActivate" not found in C:\xampp\htdocs\wordpress\wp-includes\class-wp-hook.php:292 Stack trace: #0 C:\xampp\htdocs\wordpress\wp-includes\class-wp-hook.php(316): WP_Hook->apply_filters('', Array) #1 C:\xampp\htdocs\wordpress\wp-includes\plugin.php(484): WP_Hook->do_action(Array) #2 C:\xampp\htdocs\wordpress\wp-admin\plugins.php(193): do_action('activate_templa...') #3 {main} thrown in C:\xampp\htdocs\wordpress\wp-includes\class-wp-hook.php on line 292
以下是我的文件。感谢任何帮助。
// inc/template-plugin.php
<?php
namespace Inc;
class TemplatePluginActivate
{
public static function activate() {
flush_rewrite_rules();
}
}
//模板-plugin.php
defined( 'ABSPATH' ) or die( 'You can\t access this file.' );
if ( file_exists( dirname( __FILE__ ) . '/vendor/autoload.php' ) ) {
require_once dirname( __FILE__ ) . '/vendor/autoload.php';
}
use Inc\TemplatePluginActivate;
if ( !class_exists( 'TemplatePlugin' ) ) {
class TemplatePlugin
{
//
}
}
require_once plugin_dir_path( __FILE__ ) . 'inc/template-plugin-activate.php';
register_activation_hook( __FILE__, array( 'TemplatePluginActivate', 'activate' ) );
require_once plugin_dir_path( __FILE__ ) . 'inc/template-plugin-deactivate.php';
register_activation_hook( __FILE__, array( 'TemplatePluginDeactivate', 'deactivate' ) );
// composer.json
{
"name": "author/template-plugin",
"description": "awesome starter plugin example",
"type": "project",
"license": "GPL",
"authors": [
{
"name": "author",
"email": ""
}
],
"minimum-stability": "dev",
"require": {},
"autoload": {
"psr-4": {"Inc\": "./inc"}
}
}
问题出在 register_activation_hook()
。
您的回调是 TemplatePluginActivate
class 的 activate()
方法。一旦您引入名称空间,就需要对其进行更新。 class 将是 Inc\TemplatePluginActivate
.
register_activation_hook( __FILE__, array( 'Inc\TemplatePluginActivate', 'activate' ) );
此外,您的停用操作也是 运行 激活。你需要的钩子是 register_deactivation_hook()
.