skip to Main Content

When I try to create a main menu, with this line

add_menu_page("page title","menu name",10,"test-slug");

WordPress loads just fine and displays the new menu, but when I try to add a submenu,

add_menu_page("page title","menu name",10,"test-slug");
add_submenu_page("test-slug","sub title","sub menu",10,"test-sub-slug");

only an empty page is displayed, right after half a second. Seems like a php-error, but I can’t see the error. Does anyone see what I am doing wrong?

2

Answers


  1. Chosen as BEST ANSWER

    Problem solved. add_submenu_page() obviously must be called using a hook. Hence, this way it works and will not cause Wordpress to crash:

    function dp_menu_item()
    {
        add_menu_page("page title","menu name","manage_options","mypage.php");
        add_submenu_page("edit.php?post_type=cpt_people","sub title","sub menu","manage_options","mysubpage.php");
    }
    add_action("admin_menu", "dp_menu_item");
    

  2. Try the code below in your functions.php file. It should work correctly.

    add_menu_page(
        'Menu Page Title', // page title
        'Menu Menu Text', // menu link text
        'manage_options', // capability to access the page
        'menu_slug', // page URL slug
        'menu_callback_function', // callback function to display the content on options page
        'dashicons-format-status', // menu icon
        2 // priority
    );
    
    add_submenu_page(
        'menu_slug', // page URL slug
        'Sub Menu Title', // page title
        'Sub Menu Text', // menu link text
        'manage_options', // capability to access the page
        'submenu_slug', // page URL slug
        'submenu_callback_function', // callback function to display the content on options page
        1 // priority
    );
    
    function menu_callback_function(){
        echo "This is menu page...";
    }
    
    function submenu_callback_function(){
        echo "This is submenu page...";
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search