• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

I Like Kill Nerds

The blog of Australian Front End / Aurelia Javascript Developer & brewing aficionado Dwayne Charrington // Aurelia.io Core Team member.

  • Home
  • Aurelia 2
  • Aurelia 1
  • About
  • Aurelia 2 Consulting/Freelance Work

How to Remove WordPress Menu Items (Including Those Created by Plugins)

Wordpress · September 29, 2021

WordPress comes with a lot of stuff out of the box. Throw in some plugins, and it gets even noisier. Next minute, your menu sidebar in admin looks like that drawer in the kitchen you shove everything into (known in Australia as a crap draw).

Now, you’ll want to hide some menu items for particular roles or even users (whatever floats your boat). The first hurdle can be knowing what each item is registered as (the slug matters). The core menu items like plugins and tools are easy enough, but custom menu items like “Custom Fields” and “Elite Video Player” — you need to know the slugs these were registered using.

For this, we’ll “pretty print” the menu array (where all menu items live) to discover the slugs. Using this handy little snippet (because I can never remember how to do it using PHP), you’ll get a nice extensive list of menu items.

add_action( 'admin_init', 'get_main_menu_items' );

function get_main_menu_items() {
    echo '<pre>' . print_r( $GLOBALS[ 'menu' ], TRUE) . '</pre>';
}

This will add an enormous ugly printed array inside of your admin panel. What we are looking for is index 2 of the displayed values. This is the slug we need to provide to remove_menu_page

<pre>Array
(
    [2] => Array
        (
            [0] => Dashboard
            [1] => read
            [2] => index.php
            [3] => 
            [4] => menu-top menu-top-first menu-icon-dashboard menu-top-last
            [5] => menu-dashboard
            [6] => dashicons-dashboard
        )

    [4] => Array
        (
            [0] => 
            [1] => read
            [2] => separator1
            [3] => 
            [4] => wp-menu-separator
        )

    [5] => Array
        (
            [0] => Posts
            [1] => edit_posts
            [2] => edit.php
            [3] => 
            [4] => menu-top menu-icon-post open-if-no-js menu-top-first
            [5] => menu-posts
            [6] => dashicons-admin-post
        )

    [5.46068] => Array
        (
            [0] => Raw Queue
            [1] => manage_options
            [2] => raw-queue
            [3] => Raw Queue
            [4] => menu-top menu-icon-generic toplevel_page_raw-queue
            [5] => toplevel_page_raw-queue
            [6] => dashicons-admin-generic
        )

    [6] => Array
        (
            [0] => Pinning Queue
            [1] => manage_options
            [2] => pinning-queue
            [3] => Pinning Queue
            [4] => menu-top menu-icon-generic toplevel_page_pinning-queue
            [5] => toplevel_page_pinning-queue
            [6] => dashicons-admin-generic
        )

    [10] => Array
        (
            [0] => Media
            [1] => upload_files
            [2] => upload.php
            [3] => 
            [4] => menu-top menu-icon-media
            [5] => menu-media
            [6] => dashicons-admin-media
        )

Now, in the case of our custom plugin menu item for Elite Video Player, we see this:

    [100] => Array
        (
            [0] => Elite Video Player
            [1] => manage_options
            [2] => elite_player_admin
            [3] => Elite Video Player Admin
            [4] => menu-top toplevel_page_elite_player_admin menu-top-first menu-top-last
            [5] => toplevel_page_elite_player_admin
            [6] => dashicons-video-alt3
        )

We can see index 2 has a value of “elite_player_admin” this is what we’ll pass to the remove_menu_page function.

Putting it all together

function theme_hide_menu_items() {
    remove_menu_page( 'options-general.php' ); // Remove WordPress settings menu
    remove_menu_page( 'elite_player_admin' );
}

add_action('admin_init', 'theme_hide_menu_items'), 999);
add_action('admin_menu', 'theme_hide_menu_items', 999);

Now, here is where things get interesting. Most examples you will encounter only use the admin_init hook, but I also had to use the admin_menu hook to get items to remove. Even though I had a massive priority of 999, some items were stubborn and adding that second action hook removed them. Try just using one or the other first, my problem was probably something else and I used a machine gun to hammer in some nails.

Removing menu items based on user

This is a super-specific use case, but I had a site where only a particular user could see specific items. I could have used custom permissions/roles, but as you might be aware, that is a lot of messing around. I just wanted to remove menu items if the user wasn’t admin-user in your case, it might be one or more users.

You could rewrite this to check permissions/roles if you prefer, but this worked for my use case, even if it isn’t the right way to go about it.

function theme_hide_menu_items() {
  	$user = wp_get_current_user();
  
    if ($user->user_login != 'admin-user') {
    	remove_menu_page( 'options-general.php' ); // Remove WordPress settings menu
    	remove_menu_page( 'elite_player_admin' );
    }
}

add_action('admin_init', 'theme_hide_menu_items'), 999);
add_action('admin_menu', 'theme_hide_menu_items', 999);

Hopefully, this helped and inspired you.

Dwayne

Leave a Reply Cancel reply

0 Comments
Inline Feedbacks
View all comments

Primary Sidebar

Popular

  • Testing Event Listeners In Jest (Without Using A Library)
  • How To Get The Hash of A File In Node.js
  • Thoughts on the Flipper Zero
  • Waiting for an Element to Exist With JavaScript
  • How To Paginate An Array In Javascript
  • How To Mock uuid In Jest
  • How To Get Last 4 Digits of A Credit Card Number in Javascript
  • How to Use Neural DSP Archetype Plugins With the Quad Cortex
  • How To Decompile And Compile Android APK's On A Mac Using Apktool
  • NBN Box Installed Inside of Garage, Where Do You Put The Modem?

Recent Comments

  • Kevmeister68 on Start-Ups and Companies That Embrace Work From Anywhere Will Be More Likely to Survive the Coming Recession in 2023
  • kevmeister68 on What Would Get People Back Into the Office?
  • Dwayne on PHP Will Not Die
  • Dwayne on How to Create a Blockchain With TypeScript
  • kevmeister68 on PHP Will Not Die

Copyright © 2023 · Dwayne Charrington · Log in

wpDiscuz