How to remove sub-pages from WordPress menu

After you have removed some of the top-level pages from your WordPress menu, you might want to edit the menu furthermore. Instead of removing the entire menu item, you may only want to remove the sub-page item which is usually shown after you click or hover over the top-level menu item.

For example, if you are a developer who has been working on the website for a client, you might want to remove WordPress plugin and theme editors which allow admins to open and edit core files for the plugin/theme they select.

Instead of warning a client and thoroughly explaining why not to mess with those files, you can easily remove the link from their menu.

Remove sub-pages from the menu:

  1. Open functions.php
  2. Copy and paste the following code:
  3. add_action( 'admin_menu', 'my_remove_menus', 999 );
    function my_remove_menus() {
    remove_submenu_page( 'themes.php', 'theme-editor.php' );
    }
  4. Save changes

The code shown above will remove the aforementioned editor from the menu pages and your client won’t be tempted into changing the code he probably doesn’t understand in the first place.

Maybe you are wondering if you can remove other pages from the sub-menu as well – of course, you can and the recipe is the same. You will need to find out the page slug and edit a code a little bit.

remove_submenu_page( $parent_slug, $menu_slug );

Find page slugs for the code:

Finding those page slugs couldn’t be easier. First, you will need to find parent slug and then the sub-menu page slug. Simply navigate to the menu item you want to remove and hover the mouse pointer over it. On the bottom of your browser, you will be able to see the entire URL leading to the page. For example, if you want to remove Writing Settings from the menu, you will have to navigate to Settings and hover over it. You will find out that the page slug for that parent menu item is: http://www.yourdomain.com/wp-admin/options-general.php

Now go to “Writing” menu link and hover over it. On the bottom of your browser, you will find that the URL is: http://www.yourdomain.com/wp-admin/options-writing.php

Now you should edit the function with slugs you have found and add the function in the code shown in this article. In this example where we’re removing the Writing from the General Settings, the function would look like this:

remove_submenu_page( ‘options-general.php’, ‘options-writing.php’ );

Don’t forget to save the changes and check the menus to see if everything is working fine for you. Following the same formula, you can easily hide any menu item you want.

As with top-level menus, you can easily modify the code to make it hide submenu items for anyone but the admin. If you want the admin to be able to see those hidden links, you will need to modify the top part of the code:

Instead of:

add_action( 'admin_menu', 'my_remove_menus', 999 );

Add:

if (!current_user_can( 'manage_options' )) {
add_action( 'admin_menu', 'my_remove_menus', 999 );
}

That’s it. If you combine functions for removing top level and submenu pages, you can quickly manipulate admin menu and decide which users will see which menu parts. But don’t forget – even if you make those changes, users who are able to find full paths to hidden pages will still be able to access them by navigating to the URL.

If this short tutorial was interesting to you, we suggest that you check the WordPress basics and find more WordPress tutorials that will help you shape your site the way you wanted it to be.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

Your email address will not be published. Required fields are marked *