How to Remove WordPress Admin Menu Items Programmatically Without Plugin

How to Remove WordPress Admin Menu Items programatically

WordPress dashboard menus are a very useful way to get around the different sections of the admin dashboard but sometimes they can be a nuisance or unwanted.

In particular, when you are using WordPress as a content management system for creating a client website as opposed to blogging you need to remove WordPress admin menu items that are not needed.

One of the easiest ways to remove WordPress admin menu items is using the freely available plugins that can hide the admin menu items by user role, or conditionally.

When Not to Remove WordPress Admin Menu Items Using a Plugin

The drawbacks of using plugins to remove WordPress admin menu items may be over-reliance on third-party plugin developers for consistent updates.

Third-Party Plugins Risk

In this case, when you want to take control of which menus are displayed in your WordPress dashboard without using a plugin, this tutorial is the best guide for you.

Constant Updates

Another reason to remove WordPress admin menu items programmatically is the constant updates you have in all WordPress plugins you install.

These updates are important since they improve the user experience, performance, and security but they can be too cumbersome especially when you are dealing with a large number of client’s websites.

Several Plugin Admin Menus

When you have so many plugins installed on your site most come with admin menus and you may want to remove the menus since you are not constantly using the admin menus.

In this post, I will illustrate how to remove WordPress menu items without using a plugin.

For you to implement this in your WordPress site, you need some little experience editing WordPress sites, especially adding code snippets to the functions.php theme file.

Remove WordPress Admin Menu Items Programmatically

I like to begin with a quick summary since it makes it easier for readers looking for a quick solution in a step-by-step guide.

The following are the steps you should take to remove WordPress admin menu items.

  1. Login into your WordPress site and identify the admin menu items you want to remove
  2. Create an action hook that hooks on the admin_menu event with a callback function that has the logic to remove WordPress admin menus from your dashboard.
  3. The callback function should use the remove_menu_page or remove_submenu_page() WordPress functions to remove each of the targeted admin menu items. For example, you can remove the widgets menu from the themes menu using this code remove_submenu_page( 'themes.php', 'widgets.php'); or remove the dashboard main menu with this code – remove_menu_page('index.php');
  4. Alternatively, you can use the unset() php function and access the values of the global submenu array to remove each of the targeted WordPress admin menu items.
  5. Add this code snippet to the theme functions.php and update the changes. This will successfully remove the WordPress admin menu items without using a plugin.
  6. You can also add this code to a function in your custom plugin and it will work as it works in the theme functions.php

How to Remove WordPress Admin Menus Without Plugin

The following is the detailed step-by-step approach to removing WordPress admin menu items by adding code instead of using another plugin.

 Identify Admin Menu Items Slug

Log into your WordPress dashboard and check for the admin menu items that you want to remove.

As you can see in this example the main dashboard menu has the slug index.php.

This slug is important since it is one of the parameters you add to the remove_submenu_page() function, as I will illustrate shortly.

remove wordpress admin menu

Understand How to Remove Page and Remove Submenu Page Functions Works

Remove Page Function

The remove_menu_page function comes with only one parameter which is the page slug and it removes the top-level admin menu. The parameter is a string since it’s the page slug that is obviously a string and the general code should be as follows:

remove_menu_page( string;$menu_slug;)

Remove Subpage Function

The remove_submenu_page  function comes with two-parameter the first which is the slug of the parent menu when you are removing the submenus and the second of the slug of the submenu you want to remove.
The general code should be as follows:

remove_submenu_page( string $menu_slug, string $submenu_slug )

Create an Action Hook to Remove Admin Menu and or Submenu Items

The following is an example of an action hook that hooks on the admin_menu event and has a callback function we have named ‘remove_admin_menu_items’

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

Create the Callback Function to Remove Admin Menu and or Submenu Items with remove_submenu_page and remove_menu_page ()

First, in the callback function, you need to get the global menus array with the global $submenu variable if you will be using the unset() function.

Secondly, you add the remove_submenu_page or remove_menu_page () function targeting the page you want to remove as shown in the code below:

function remove_admin_menu_items() {        

    remove_submenu_page( 'menu_slug', 'submenu_slug'); 

}

Or using unset you can have the following callback function

function remove_admin_menu_items() {

        global $submenu; 

       unset($submenu['parent menu slug'][admin item submenu array index]);

 }

Example of How to Remove Updates Admin Menu Item

When you log in to your WordPress dashboard there is a menu item about the updates that are located under the main dashboard menu as shown in the image below:

remove wordpress admin menu

I will illustrate how you can remove this admin menu item using the method discussed above.

First, the parent menu slug is index.php and the slug for the updates menu is update-core.php

Remove WordPress Admin Menu Item Parent and Submenu

So you can create the action hook and callback function to remove the entire menu item using the remove_menu_page() function as follows :

// Remove the main menu item together with the subpages /submenus

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

 function remove_admin_menu_items() {

    remove_menu_page('index.php');


 }

This will remove the entire menu and the submenus as shown in the image below:

Remove WordPress Submenu Items Only

Remove WordPress Submenu Items Only

You can create the action hook and callback function to remove the submenu item using the remove_submenu_page function as follows:

//Remove the subpages or submenu

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

function remove_admin_menu_items() {

    remove_submenu_page('index.php','update-core.php' );

}

This will remove the submenu specified in the second parameter of the remove_submenu_page function, in this case, we specified it as ‘update-core.php’ and the result is as shown in the image below:

Using Unset() PHP Function

We can also use the unset function of PHP to remove the submenu items and in this case, we need to first access the global $submenu array as shown in the code below:

//add the action hook to admin_menu event

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

//callback function
function remove_admin_menu_items() {

    //Admin menu array->List of all the WordPress admin menu items are in this array
    global $submenu;

    //print the array to the screen so that we can see the admin menu items by index
    
     print('<pre>');
     print_r($submenu);
     print('<pre>');

      // we remove everything else displayed on the screen so that see only the admin menu items array
      die();

}

This code gets hold of the global submenu array and we print it to display the array of all the menus so that we can pick the index of each of the admin menu items and then remove them as we wish.

In this code, you can see the use of the pre-tags and die() function to display the code in a legible format.

Using Unset() PHP Function wordpress

Remove the WordPress Submenu Menu item using Unset

So the array index for the update-core submenu is 10. So we can go ahead and use the unset() function as follows :

// Remove the main menu item together with the subpages using unset

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

function remove_admin_menu_items() {

    global $submenu;

       unset($submenu['index.php'][0]); //remove top level menu index.php (dashboard menu - Home menu )
      unset($submenu['index.php'][10]); // remove the submenu update-core.php (updates menu)


}

The results will be the same as using the remove_submenu_page WordPress function.

Remove WordPress Admin Menu item using Unset

We can also remove the top-level page and the submenu altogether using the unset function targeting each of these pages:

Remove WordPress Admin Menu item using Unset

The code should be as follows:

//. Remove the main submenu item using unset

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

function remove_admin_menu_items() {

   global $submenu;

   unset($submenu['index.php'][10]);


}

The result will be to remove either of the pages as we have unset in the code:

How to Remove WordPress Admin Menu Items Programmatically without plugin

Wrapping up

In this post, we have extensively covered the two main ways you can use to remove WordPress admin menu items and clean up your WordPress dashboard.

If you are a WordPress developer, you can reuse this code and copy it easily checkout my gist and you can follow me on GitHub for more future updates.

I hope you can now remove those WordPress admin items you don’t want from your menu.

Similar Articles

Comments are closed.