6

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

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?

brasofilo
  • 25,496
  • 15
  • 91
  • 179
Musa Haidari
  • 2,109
  • 5
  • 30
  • 53
  • Answered on [Wordpress StackExchange](https://wordpress.stackexchange.com/questions/44270/getting-custom-admin-submenu-item-to-highlight-when-its-active#131873) – Mort 1305 May 28 '20 at 20:05

2 Answers2

6

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

derekshirk
  • 187
  • 1
  • 7
3

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;
});

brasofilo
  • 25,496
  • 15
  • 91
  • 179
  • Great answer. Only I wanted to mention to make the "Vendors" also selected, I added the class `current` to its `li` tag. – Musa Haidari May 10 '14 at 07:34
  • 1
    set global $plugin_page to your submenu slug in filter 'parent_file' – tutankhamun Mar 04 '15 at 13:54
  • 1
    Also you can add the "current" class to the "a" tag inside the "li" tag: $('#toplevel_page_nes_general_settings a') ... Otherwise when we hover it changes the icon, at least when I tried it, I needed to do this. – Nikolay Oct 07 '17 at 13:05