[英]Wordpress plugin: Call to undefined function add_menu_page()
I need some help, I've been through so many of these "similar" issues on stackoverflow and all over the web but nothing seems to be helping. 我需要一些帮助,我在stackoverflow和整个Web上经历了很多此类“类似”问题,但似乎无济于事。
I'm new to wordpress and started creating a plugin using classes. 我是wordpress的新手,并开始使用类创建插件。 when I run the plugin it throws the error "Call to undefined function add_menu_page()".
当我运行插件时,它将引发错误“调用未定义函数add_menu_page()”。 I've checked the support docs on wordpress.org but that doesn't help either ( https://codex.wordpress.org/Adding_Administration_Menus#Inserting_the_Pages )
我已经检查了wordpress.org上的支持文档,但这也无济于事( https://codex.wordpress.org/Adding_Administration_Menus#Inserting_the_Pages )
heres my code below please can you tell me what I'm doing wrong: 下面是我的代码,请您告诉我我做错了什么:
class SetupAdminMenu {
public static function add_menu($menu)
{
add_action('admin_menu', self::load_menu($menu) );
//I've used it according to the docs as well
//add_action('admin_menu', array(self, self::load_menu($menu) );
}
public static function load_menu($menu)
{
$page = add_menu_page( $menu['page_title'], $menu['menu_title'], $menu['capability'], $menu['menu_slug'],
array(self,self::load_view($menu['function'])), $menu['icon_url'], $menu['position'] );
self::load_page($page);
}
protected static function load_view($view)
{
if ( !current_user_can( 'activate_plugins' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
//The actual page to display
include_once($view);
}
protected static function load_page($page)
{
add_action( 'admin_print_styles-' . $page, 'admin_styles' );
}
}
Thanks peeps 谢谢偷看
Couple of things going on here. 这里发生了几件事。 First to just clarify, add_menu_page is to add an administration menu page for managing settings, etc.
首先要澄清的是,add_menu_page是添加用于管理设置等的管理菜单页面。
Your add action should be as follows: 您的添加操作应如下所示:
public static function add_menu($menu)
{
add_action('admin_menu', array('SetupAdminMenu', 'load_menu'));
}
The way WordPress handles actions requires you to format your add_action as follows for static class methods: WordPress处理操作的方式要求您为静态类方法设置add_action的格式如下:
add_action('admin_menu', array('ClassName', 'methodname'), $priority, $numVars);
There are two optional variables: 有两个可选变量:
Next, the class method with "add_menu_page" is pretty off. 接下来,带有“ add_menu_page”的类方法已经相当完善了。 To my knowledge admin_menu hook does not return any variables by default and even if it did, why do you want the existing menu when trying to add new items?
据我所知,admin_menu挂钩默认情况下不会返回任何变量,即使这样做,为什么在尝试添加新项目时仍要使用现有菜单? You pass those variables yourself:
您自己传递这些变量:
public static function load_menu()
{
$page_title = 'My New Page';
$menu_title = 'New Page';
$capability= 'manage_options'; //An administrator only capability
$menu_slug = 'my-new-page';
$callback = array('SetupAdminMenu', 'load_view');
$menu_icon = ''; //use default
$menu_position = 81 //After settings
add_menu_page( $page_title, $menu_title, $capability, $menu_slug,
$callback, $menu_icon , $menu_position);
}
Your self::load_page($page); 您自己:: load_page($ page); is redundant.
是多余的。 The action you use in that function does nothing.
您在该功能中使用的操作没有任何作用。 The callback is what is displayed when the menu item is clicked.
回调是单击菜单项时显示的内容。 No reason to make that protected, it would likely fail anyway because of the way class methods are called from hooks within WordPress.
没有理由对其进行保护,由于从WordPress中的钩子调用类方法的方式,它无论如何都可能会失败。
public static function load_view() // Removed $view because you need to define this yourself.
{
if ( !current_user_can( 'activate_plugins' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
//The actual page to display
//include_once('/path/to/your/template');
//which should have php, html for what you want to have appear.
//To test that the rest of the code is working... just put:
echo 'Hello World!';
}
This link has a list of a bunch of "how tos" on WP Plugin development. 该链接列出了WP插件开发中的一系列“操作方法”。
alright, so I've done some more reading and playing and below is my answer, might help some. 好吧,所以我做了更多的阅读和游戏,下面是我的答案,可能会有所帮助。
class SetupAdminMenu {
private static $menu = array();
public static function add_menu()
{
add_action('admin_menu', array('SetupAdminMenu', 'load_menu') );
}
public static function add_sub_menu()
{
add_action('admin_menu', array('SetupAdminMenu', 'load_sub_menu') );
}
the method below is what I was missing initially, because you cannot pass an argument from the call back in add_action()
下面的方法是我最初缺少的方法,因为您无法在
add_action()
通过回调传递参数
public static function setupPage($params)
{
self::$menu = $params;
}
public static function load_menu()
{
$page = add_menu_page( self::$menu['page_title'], self::$menu['menu_title'], self::$menu['capability'],
self::$menu['menu_slug'], array('SetupAdminMenu', 'load_view'),
self::$menu['icon_url'], self::$menu['position'] );
self::load_page($page);
}
protected static function load_sub_menu()
{
$page = add_submenu_page( self::$menu['parent_slug'], self::$menu['page_title'], self::$menu['menu_title'],
self::$menu['capability'], self::$menu['menu_slug'], 'load_view');
self::load_page($page);
}
public static function load_view()
{
if ( !current_user_can( 'activate_plugins' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
//The actual page to display
include_once(self::$menu['page']);
}
protected static function load_page($page)
{
add_action( 'admin_print_styles-' . $page, array('SetupAdminMenu', 'admin_styles') );
}
public static function admin_styles()
{
wp_register_style( 'admin_styles', plugins_url('css/admin.css', __FILE__) );
wp_enqueue_style( 'admin_styles' );
}
}
because add_action()
is used so many times I've removed add_menu()
and add_sub_menu()
from this class and added the method below to may base class to be called with the necessary parameters for any actions 因为
add_action()
被使用了很多次,所以我从此类中删除了add_menu()
和add_sub_menu()
,并将下面的方法添加到may base类中,该基类可以使用任何操作的必要参数进行调用
public static function addAction($action, $callback)
{
add_action($action, $callback);
}
might not be 100% the best way to do it yet but the above currently works. 可能不是100%的最佳方法,但以上方法目前有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.