0

I'm trying to create a generic function that removes menu pages in admin dashboard wordpress. This is my attempt...

function remove_menus(){

    //Find admin...
    $admin_url = get_admin_url();

    //Find php-files in admin path
    //and create an array of them
    $admin_files = array();
    foreach (glob("*.php") as $file) {
            if($file == '.' || $file == '..') continue;
            $admin_files[] = $file;
    }

    //Remove all menupages for all files in wp-admin folder
    foreach($admin_files as $af) {
        remove_menu_page( $af );    
    }

    //Get all registered post types
    // types will be a list of the post type names
    $types = get_post_types();

    //Remove pages for all registered post types as well
    foreach( $types as $type ) {
        remove_menu_page( 'edit.php?post_type=' . $type);
    }

    //Get plugins and remove them from menu
    $plugins = get_plugins();
    foreach($plugins as $p) { 
        remove_menu_page( strtolower($p['Name'] ) );
    }

}
add_action( 'admin_init', 'remove_menus' ,99);

The part I wonder about is this:

 //Get plugins
 $plugins = get_plugins();
 foreach($plugins as $p) { 
     remove_menu_page( strtolower($p['Name'] ) );
 }

This would only remove plugins where name of the plugin is set like admin.page={name of plugin}

Example: remove_menu_page('duplicator'); would remove duplicator plugin because admin.page=duplicate

remove_menu_page('Yoast SEO'); would NOT remove Yoast SEO plugin because the actual slug is admin.php?page=wpseo_dashboard (and not admin.php?page=yoastseo )

To remove this plugin manually you would have to

remove_menu_page('wpseo_dashboard');

Is there a way to remove plugins in dashboard - where you cannot identify the slug by the plugins name?

1

I think what you'll want to do is iterate over the $_GLOBALS['menu'] array. This should give you a list of all the items in the admin menu so you know what the hooks are that need to be removed.

There are more details and good information in this answer: https://wordpress.stackexchange.com/a/136064/9080

The appropriate hook/slug can be found in the array element with key 2, according to that answer.

You may also find this tutorial useful Customizing Your WordPress Admin

  • Thanks a lot! The weird thing is that I read the first link you supplied, but obviously I read it too fast ;-) Thanks! – bestprogrammerintheworld Oct 8 '15 at 3:14
  • 1
    You're welcome, glad it helped! :) – Chris - SevenSpark Oct 8 '15 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.