WordPress and Call to undefined function add_menu_page()

28,264

Solution 1

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

Solution 2

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

Share:
28,264

Related videos on Youtube

Gazillion
Author by

Gazillion

Updated on April 23, 2020

Comments

  • Gazillion
    Gazillion almost 4 years

    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:

    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 :)

  • Gazillion
    Gazillion about 13 years
    Thanks, my problem was that I wasn't putting the call into a function.
  • GabLeRoux
    GabLeRoux almost 11 years
    I had to work with someone else's code and it was working on production server but not on my development server. I changed add_action('init', 'my_menu'); to add_action('admin_menu', 'my_menu'); and it worked on both servers, thanks :)
  • Rouzbeh Zarandi
    Rouzbeh Zarandi over 3 years
    @GabLeRoux why ? same thing here and thanks for save me
  • Michael Hicks
    Michael Hicks over 2 years
    Thank you @GabLeRoux, I had same issue and spent hours trying to get it to work. The admin_menu hook worked great!

Related