hakkim
hakkim

Reputation: 658

How to add menu locations as admin dashboard menu in WordPress

I have around 5 custom menu locations in wordpress. And Now I need to make each of the location as a main side bar navigation in wordpress admin panel. I have tried with add_action method. And this is my code snippet.

add_action('admin_menu', 'sep_menuexample_create_menu' );
function sep_menuexample_create_menu() {
//create custom top-level menu
add_menu_page( 'My Plugin Settings Page', 'Menu Example Settings','manage_options','navmenu.php', 'sep_menuexample_settings_page',screen_icon('edit'));
}
function sep_menuexample_settings_page(){

}

How can I achieve it?

Upvotes: 3

Views: 2199

Answers (1)

brasofilo
brasofilo

Reputation: 26065

That's only possible with jQuery. Create the admin menus and submenus that you want, and add jQuery in admin_head to run in all admin pages.

It's a matter of finding your admin menu anchors and changing its href attribute. In this example, the admin menus are modified to point to nav-menus.php?action=edit&menu=MENU_ID:

add_action( 'admin_menu', function() {
    add_menu_page( 
        'My custom menu Settings', 
        'Menus', 
        'manage_options', 
        'my-menus', 
        function(){ echo 'This does not show up'; },
        null,
        25
    );

    add_submenu_page( 
        'my-menus' , 
        'My custom submenu-1', 
        'Menu 1', 
        'manage_options', 
        'my-menus', // <---- Same as main menu, change to "sub-menu1" to see effect
        function(){}
    );
    add_submenu_page( 
        'my-menus' , 
        'My custom submenu-2', 
        'Menu 2', 
        'manage_options', 
        'sub-menu2', 
        function(){}
    );
});

# See http://stackoverflow.com/questions/5673269/ for <<<HTML usage
add_action( 'admin_head', function (){
    echo <<<HTML
    <script type="text/javascript">
        jQuery(document).ready( function($) {
            topmenu = $('#toplevel_page_my-menus');
            nav_menu1 = 'nav-menus.php?action=edit&menu=1';
            nav_menu2 = 'nav-menus.php?action=edit&menu=2';
            topmenu.find('a[href="admin.php?page=my-menus"]').attr('href',nav_menu1);  
            topmenu.find('a[href="admin.php?page=sub-menu2"]').attr('href',nav_menu2);
        });     
    </script>
HTML;
});

Upvotes: 1

Related Questions