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:

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

    [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
                )

        )

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:

and put this along with permissions checking in:

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

Leave a Reply