基于WP插件样板的插件返回错误”警告(call_user_func_array()期望参数1为有效的回调”)

【基于WP插件样板的插件返回错误” 警告(call_user_func_array()期望参数1为有效的回调” )】我正在基于Wordpress插件样板(面向对象的编程)构建Wordpress插件, 并且在调用在自定义管理仪表板菜单中显示插件管理页面的函数时遇到了困难。
在我的课堂上, 我呼吁我的自定义菜单方法, 它在菜单显示出色时起作用。

private function define_admin_hooks() {$plugin_admin = new myPlugin_Admin( $this-> get_plugin_name(), $this-> get_version() ); $this-> loader-> add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' ); $this-> loader-> add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' ); // Load CPTs $this-> loader-> add_action( 'admin_init', $plugin_admin, 'create_cpt_person' ); $this-> loader-> add_action( 'admin_menu', $plugin_admin, 'setup_admin_menus' ); $this-> loader-> add_action( 'parent_file', $plugin_admin, 'set_current_menu' ); }

然后执行我的setup_admin_menus方法。
public static function setup_admin_menus() {$page_title = 'myPlugin'; $menu_title = 'myPlugin'; $capability = 'manage_options'; $menu_slug= 'myplugin_admin'; $function= 'display_admin_page'; // This is what triggers the showing of my admin page once I click on the parent menu (as if you were clicking on "Pages" in your admin dashboard) $icon_url= 'dashicons-admin-page'; $position= 3; add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position ); $submenu_pages = array( // Post Type :: Person array( 'parent_slug'=> 'myplugin_admin', 'page_title'=> 'People', 'menu_title'=> 'People', 'capability'=> 'read', 'menu_slug'=> 'edit.php?post_type=person', 'function'=> null ) ); // Add each submenu item to custom admin menu. foreach($submenu_pages as $submenu){ add_submenu_page( $submenu['parent_slug'], $submenu['page_title'], $submenu['menu_title'], $submenu['capability'], $submenu['menu_slug'], $submenu['function'] ); } }

这就是所谓的函数:
public static function display_admin_page() {echo '< div class="wrap"> '; echo '< h2> Welcome To WordPets< /h2> '; echo '< p> This is the custom admin page created from the custom admin menu.< /p> '; echo '< /div> < !-- end .wrap --> '; echo '< div class="clear"> < /div> '; }

但是, 每当我转到插件的管理页面时, 都会收到警告:call_user_func_array()期望参数1为有效的回调, 未找到函数’ display_admin_page’ 或/ Users / Lazhar / dev / wp-includes / plugin中的函数名称无效。 PHP在第496行, 除了错误消息外, 没有显示任何内容。
任何帮助将不胜感激:)
#1尝试将display_admin_page()方法放在类之外
function display_admin_page() {echo '< div class="wrap"> '; echo '< h2> Welcome To WordPets< /h2> '; echo '< p> This is the custom admin page created from the custom admin menu.< /p> '; echo '< /div> < !-- end .wrap --> '; echo '< div class="clear"> < /div> '; }

#2另一种方法(而不是将函数放置在类之外)是通过将类方法作为数组传递:
$page_title = 'myPlugin'; $menu_title = 'myPlugin'; $capability = 'manage_options'; $menu_slug= 'myplugin_admin'; $function= array($this, 'display_admin_page'); //This will properly get the class method function name

这是引用函数(类方法)的正确方法。

    推荐阅读