How to manually set a custom admin submenu selected?

I am developing my first WordPress plugin.
Lets say it has following admin pages:

  • General Settings
  • Add New Vendor
  • Vendors
  • Edit Vendor

From which I do not want to dispaly the Edit Vendor in the menu and want to make it accesseable using a link in Vendors page. My menu and Vendors page are:
enter image description here

Read More

Code to generate the menu:

function nes_general_settings_view () { 
    require_once("views/admin/general_settings.php");
}

function nes_vendor_view () { 
    require_once("views/admin/vendor.php");
}

function nes_vendor_new_view () {
    require_once("views/admin/vendor_new.php");
}

function nes_vendor_edit_view () {

    require_once("views/admin/vendor_edit.php");
}

add_action("admin_menu", function () {
    add_menu_page(
        "Service",  
        "Service",  
        "manage_options",            
        "nes_general_settings",      
        "nes_general_settings_view", 
        null,                        
        4                            
    );

    add_submenu_page( "nes_general_settings", "General Settings", "General Settings", 0, "nes_general_settings", "nes_general_settings_view");      
    add_submenu_page( "nes_general_settings", "Vendors", "Vendors", 0, "nes_vendor", "nes_vendor_view");
    add_submenu_page( "nes_general_settings", "New Vendor", "New Vendor", 0, "nes_vendor_new", "nes_vendor_new_view");
    add_submenu_page( "nes_fake_id", "Edit Vendor", "Edit Vendor", 0, "nes_vendor_edit", "nes_vendor_edit_view");       
});

And Code to generate the link to Edit Vendor page:

<a href="<?=admin_url("admin.php?page=nes_vendor_edit")?>">Edit</a>

But when I am in Edit Vendor page, my menu is not selected.
enter image description here

How to set Service > Vendors submenu selected, when I am on Edit Vendor?

Related posts

Leave a Reply

2 comments

  1. In the case where you’re sub menus are custom post types, you can easily define them as sub-menu-items of a custom menu by setting the custom post type parameter of “show_in_menu” as the slug that you defined when creating your custom-post-type. Doing so this way will retain the default menu open/close and highlighting of current sub-menu item in the admin menu.

    For example:

    /* ————————————————————————— */
    /* Content Types Menu Item
    /* ————————————————————————— */
    
    add_menu_page(
        'Content Types',
        'Content Types',
        'manage_options',
        'custom-content', // menu slug
        'ds_create_new_submenu',
        'dashicons-editor-table',
        null
    );
    

    And then in your custom post type arguments define the show_in_menu to match the menu slug.

    $args = array(
        'labels'                => $labels,
        'menu_icon'             => 'dashicons-format-status',
        'capability_type'       => 'manage_options',
        'show_in_menu'          => 'custom-content'
    );
    

    Code examples shortened for simplicity-sake

  2. This has to be done with jQuery to add the proper classes to the admin menu.

    Use the following to print the script:

    # Grab the slug to print only in this screen
    $hook = add_submenu_page( 
        null, # better than fake id
        "Edit Vendor", 
        "Edit Vendor", 
        'manage_options', # IMPORTANT, don't use levels (0-9), they're deprecated
        "nes_vendor_edit", 
        function() {
            echo '<h1>nes_vendor_edit_view</h1>';
        }
    );
    
    add_action( "admin_footer-$hook", function()
    {
        # http://stackoverflow.com/questions/5673269/what-is-the-advantage-of-using-heredoc-in-php
        echo <<<HTML
            <script type="text/javascript">
            jQuery(document).ready( function($) {
                $('#toplevel_page_nes_general_settings')
                    .addClass('current wp-has-current-submenu wp-menu-open');
            });     
            </script>
    HTML;
    });