I’m trying to remove a menu item and tried several approaches, but have not had any luck. It was created from a FAQ plugin.
If I use this line:
remove_menu_page( 'edit.php?post_type=question' );
It will remove the main menu, however I only want to remove two sub-menu items contained inside that edit.php?post_type=question
URL address.
For the item I want to remove, the path in the URL bar gives me the following:
edit-tags.php?taxonomy=faq-topic&post_type=question
and
edit-tags.php?taxonomy=faq-tags&post_type=question
I have tried several solutions including remove_menu_page
and remove_submenu_page
to no avail.
What has failed so far:
remove_submenu_page( 'edit.php?post_type=question', 'edit-tags.php?taxonomy=faq-topic&post_type=question');
remove_menu_page( 'edit-tags.php?taxonomy=faq-tags') ;
remove_menu_page( 'edit-tags.php?taxonomy=faq-tags&post_type=question') ;
remove_submenu_page( 'edit.php?post_type=question', 'edit-tags.php?taxonomy=faq-topic&post_type=question');
remove_submenu_page( 'edit.php', 'edit-tags.php?taxonomy=faq-topic&post_type=question');
remove_submenu_page( 'edit.php', 'edit-tags.php?taxonomy=faq-topic');
remove_submenu_page( 'edit.php', 'edit-tags.php');
2 s
function remove_submenu() {
remove_submenu_page( 'edit.php', 'edit-tags.php?taxonomy=faq-topic&post_type=question' );
remove_submenu_page( 'edit.php', 'edit-tags.php?taxonomy=faq-tags&post_type=question' );
}
add_action( 'admin_menu', 'remove_submenu', 999 );
Please read the Codex. remove_submenu_page()
need two parameters and the right hook.
And very important: Use a very, very, very high priority in your hook! If you use a low priority, your function will be executed before the menus will be added. So there is no menu to remove. If you use a high priority, there is a good chance that your function will be executed after the menus was added.
This could be the tricky part.
UPDATE
After installing and inspecting the plugin, I found the solution. There are several issues and a few tricky parts.
The submenus are not added with add_submenu_page()
, they are added with a custom post type. A simple search by add_submenu_page()
, copy the menu slugs and removing the menus have to fail. I have to search for the cpt slug and use it.
After global $submenu; var_dump( $submenu );
I get this output
[more elements]
'edit.php?post_type=question' =>
array (size=7)
5 =>
array (size=3)
0 => string 'FAQs' (length=4)
1 => string 'edit_posts' (length=10)
2 => string 'edit.php?post_type=question' (length=27)
10 =>
array (size=3)
0 => string 'Neue FAQ' (length=8)
1 => string 'edit_posts' (length=10)
2 => string 'post-new.php?post_type=question' (length=31)
15 =>
array (size=3)
0 => string 'FAQ Titel' (length=9)
1 => string 'manage_categories' (length=17)
2 => string 'edit-tags.php?taxonomy=faq-topic&post_type=question' (length=55)
16 =>
array (size=3)
0 => string 'FAQ Tags' (length=8)
1 => string 'manage_categories' (length=17)
2 => string 'edit-tags.php?taxonomy=faq-tags&post_type=question' (length=54)
[ more elements ]
Now it was easy to remove the submenus with edit.php?post_type=question
as menu slug and edit-tags.php?taxonomy=faq-topic&post_type=question
/ edit-tags.php?taxonomy=faq-tags&post_type=question
as submenu slug.
If you watch carefully, the ampersand (&) is a html entity. It’s not possible just to copy the url part and insert it. So you cannot remove a submenu page with a un-encoded url, it have to be url-encoded.
And here is the final code:
add_action( 'admin_menu', 'remove_faq_subpages', 999 );
function remove_faq_subpages() {
$ptype="question";
remove_submenu_page( "edit.php?post_type={$ptype}", "edit-tags.php?taxonomy=faq-tags&post_type={$ptype}" );
remove_submenu_page( "edit.php?post_type={$ptype}", "edit-tags.php?taxonomy=faq-topics&post_type={$ptype}" );
}