我正在构建一个基于Wordpress插件Boilerplate(面向对象编程)的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成为有效的回调,函数&lt; display_admin_page&#39;在第496行/Users/Lazhar/dev/wp-includes/plugin.php中未找到或无效的函数名称,除了错误消息外,它没有显示任何内容。
非常感谢任何帮助:)
答案 0 :(得分:0)
尝试将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>';
}
答案 1 :(得分:0)
另一种方法(而不是将函数放置在类之外)是通过将类方法作为数组传递:
$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
这是引用函数(类方法)的正确方法。