php WordPress 和调用未定义的函数 add_menu_page()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5598971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WordPress and Call to undefined function add_menu_page()
提问by Gazillion
I recently got into WordPress plugin development and I would like to add a menu page (the links in the left hand side menu). Previous SO questions and the WordPress codex say that it's as simple as calling:
我最近进入 WordPress 插件开发,我想添加一个菜单页面(左侧菜单中的链接)。以前的 SO 问题和 WordPress 法典说它就像调用一样简单:
add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );
However when I try this in my plugin setup file it tells me that the function is undefined:
但是,当我在插件设置文件中尝试此操作时,它告诉我该函数未定义:
PHP Fatal error: Call to undefined function add_menu_page()
This seems like a very simple thing to do according to the documentation but I am totally baffled. Any help would be really appreciated :)
根据文档,这似乎是一件非常简单的事情,但我完全感到困惑。任何帮助将非常感激 :)
回答by Unknown_Guy
I don't know how your code looks but this is how I just tested and it worked:
我不知道你的代码看起来如何,但这是我刚刚测试的方式并且它有效:
add_action('admin_menu', 'my_menu');
function my_menu() {
add_menu_page('My Page Title', 'My Menu Title', 'manage_options', 'my-page-slug', 'my_function');
}
function my_function() {
echo 'Hello world!';
}
Take a look here http://codex.wordpress.org/Administration_Menus
回答by Vinod Dalvi
You are getting this error message because either you have used the function add_menu_page outside any hook or hooked it too early.
您收到此错误消息是因为您在任何挂钩之外使用了 add_menu_page 函数或过早地将其挂钩。
The function add_menu_page gets capability as a third argument to determine whether or not the user has the required capability to access the menu so the function is only available when the user capability is populated therefore you should use the function in the admin_menu hook as following.
函数 add_menu_page 获取功能作为第三个参数,以确定用户是否具有访问菜单所需的功能,因此该功能仅在填充用户功能时可用,因此您应该在 admin_menu 挂钩中使用该功能,如下所示。
add_action( 'admin_menu', 'register_my_custom_menu_page' );
function register_my_custom_menu_page(){
add_menu_page( __( 'Custom Menu Title' ), 'custom menu', 'manage_options', 'custom-page-slug', 'my_custom_menu_page' );
}
function my_custom_menu_page() {
echo __( 'This is custom menu page.' );
}
See the following WordPress codex page for information about it.
有关它的信息,请参阅以下 WordPress 法典页面。