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:
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.
How to set Service > Vendors submenu selected, when I am on Edit Vendor?
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:
And then in your custom post type arguments define the show_in_menu to match the menu slug.
Code examples shortened for simplicity-sake
This has to be done with jQuery to add the proper classes to the admin menu.
Use the following to print the script: