ylliX - Online Advertising Network How to hide menu in own Wordpress plugin? - Yet Another Programmer's Blog

How to hide menu in own WordPress plugin?

Sometime you want to restrict parts of your plugin using additional permissions management, since this is quite easy, hiding menu items can be a bit tricky – you need to know root menu url, and submenu url. So where to get them? Somewhere just put this:

[php]
global $submenu;
print_r($submenu);
[/php]

so you will get list of all available items (wordpress is calling them “menu slugs”) like this:

[code]
[edit.php?post_type=page] => Array
(
[5] => Array
(
[0] => All Pages
[1] => edit_pages
[2] => edit.php?post_type=page
)

[10] => Array
(
[0] => Add New
[1] => edit_pages
[2] => post-new.php?post_type=page
)

)
[/code]

and we have all stuff needed – array key is menu slug (root menu hook), array item with key 2 is sub menu slug, so to hide it just use:

[php]
remove_submenu_page(‘edit.php?post_type=page’, ‘post-new.php?post_type=page’);[/php]

and put this along with permissions checking in:

[php]
add_action(‘admin_init’, ‘acRemoveMenuPages’);
function acRemoveMenuPages() {
remove_submenu_page(‘edit.php?post_type=page’, ‘post-new.php?post_type=page’);
}[/php]

this way at each admin’s init your permission will be checked, and unnecessary items will be removed.

Leave a Reply