WordPress and Call to undefined function add_menu_page()

荒凉一梦 提交于 2019-11-30 17:14:19

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

You are getting this error message because either you have used the function add_menu_page outside any hook or hooked it too early.

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_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.

http://codex.wordpress.org/Function_Reference/add_menu_page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!