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?