I'm building a WordPress plugin and I'd like to have an edit-item page that can't be reached via the submenu (because then the item wouldn't be specified).
This resource (http://codex.wordpress.org/Adding_Administration_Menus) shows how to associate an admin page with a function, but not how to do so without adding it as a menu item.
Can this be done?
Thanks!
Yes. It is very possible to make a page cannot be reach via submenu, or even the main menu in the WP admin panel. See the code snippet below.
Hopefully, this will help.
Using add_submenu_page with a parent of NULL definitely works, however if you want to keep the non-linked page associated with a particular menu (say a custom post type menu), you have to use a variation of @Boopathi's answer:
It looks as though the two actions would cancel each other out, however remove_submenu_page does not unregister the callback function; it merely removes the link.
This way when someone is viewing your non-linked page, the correct navigation menu (our custom post type menu in this example) will still show as active.
One of the problems I found with merely adding null as the parent slug for a sub menu item is that if you're currently viewing that specific page the submenu itself won't display (at least it didn't for me (along with the page title not showing).
What I did was add an empty span element inside the menu title and use jquery to traverse the parent elements and hide it.
I have finally discovered a way to do this that isn't an ugly hack, doesn't require JS to highlight the desired menu item (and submenu item), and works for regular menus registered by plugins (@Josh's answer only works for custom post types).
Essentially, you just need to register your submenu normally, but then hook into the
'submenu_file'
filter to deregister it and optionally also set another submenu item to highlight instead.From the answers that come before me, you can see that there are many ways to do this. However, there is another way that I think may be the best.
Loading the page differently based on the value of a
$_GET
query var is one option, but it may not be what some people are looking for.The suggestions regarding
add_submenu_page()
are on the right track, but each of the previous suggestions have problems. Setting$menu_title
tonull
doesn't keep the menu item from being displayed, it just makes it so the link doesn't have any text. The link still takes up some room in the menu though, so it looks funny. Setting the$parent_slug
tonull
doesn't have this problem, but I noticed that the page's HTMLtitle
doesn't display the$page_title
text.My solution was to set
$parent_slug
to a fake menu slug, like'i_dont_exist'
. The menu item won't be displayed, and when viewing the admin screen the page title will be filled out properly.Yes, this can be done (well, technically, it would be more like registering the whole thing and then removing the menu item later), but It would just be easiest (I think) to check for parameters in the
$_GET
super-global to indicate that the user wishes to edit a specific item.For example, you could have a page that lists items to edit, and clicking 'edit' only adds the item's ID to the current URL(query-string).
In the function that displays this page, if ID is defined, give them the page to edit that item.
Otherwise, give them the list view. That's how posts, pages, and other custom post types do it.