Reordering and removing left admin menu items
Clone the administrator user role
//https://www.kvcodes.com/2015/04/wordpress-clone-user-role-to-create-new-role/ //https://nazmulahsan.me/clone-or-add-new-user-roles/ add_action('init', 'cloneUserRole'); function cloneUserRole() { global $wp_roles; if (!isset($wp_roles)) $wp_roles = new WP_Roles(); $adm = $wp_roles->get_role('administrator'); // Adding a new role with all admin caps. $wp_roles->add_role('client', 'Client', $adm->capabilities); }
Modify the newly cloned user
// https://www.wpmayor.com/how-to-remove-menu-items-in-admin-depending-on-user-role/ add_action( 'admin_init', 'my_remove_menu_pages' ); function my_remove_menu_pages() { global $user_ID; if ( current_user_can( 'client' ) ) { remove_menu_page( 'tools.php' ); // Tools remove_menu_page( 'users.php' ); // Users remove_submenu_page( 'index.php', 'update-core.php' ); // Dashboard - Updates remove_submenu_page( 'edit.php', 'edit-tags.php?taxonomy=post_tag' ); // Posts - Tags remove_submenu_page( 'themes.php', 'theme-editor.php' ); // Appearance - Editor remove_submenu_page( 'plugins.php', 'plugin-editor.php' ); // Plugins - Editor } }
Activate a new user role through a plugin.
I am working on a custom functions plugin I call AWP Custom Functions (Advanced WordPress https://www.facebook.com/groups/advancedwp/ as it was Devin Walker through AWP who first got me hooked on the concept of a custom functions plugin. )
To activate a new user role through a plugin add something like the following code to your main plugin file.
// On plugin activation create following user role. //https://codex.wordpress.org/Function_Reference/add_role // https://codex.wordpress.org/Function_Reference/register_activation_hook // FILE = path to the main plugin file inside wp-content/plugins directory. A full path will work. // function: rename it to plugin file name. function awpcustomfunctions_activate() { add_role( 'custom-client', 'Custom Client', array( 'read' => true ) ); } register_activation_hook( __FILE__, 'awpcustomfunctions_activate' );
Reordering the left admin menu
/********************* * re-order left admin menu **********************/ function reorder_admin_menu( $__return_true ) { return array( 'index.php', // Dashboard 'edit.php?post_type=page', // Pages 'edit.php', // Posts 'upload.php', // Media 'themes.php', // Appearance 'separator1', // --Space-- 'edit-comments.php', // Comments 'users.php', // Users 'separator2', // --Space-- 'plugins.php', // Plugins 'tools.php', // Tools 'options-general.php', // Settings ); } add_filter( 'custom_menu_order', 'reorder_admin_menu' ); add_filter( 'menu_order', 'reorder_admin_menu' );
Removing top level and sub menu left admin menu items.
/* * Remove top level and sub menu admin menus */ function remove_admin_menus() { remove_menu_page( 'index.php' ); // Dashboard remove_menu_page( 'edit.php' ); // Posts remove_menu_page( 'upload.php' ); // Media remove_menu_page( 'edit.php?post_type=page' ); // Pages remove_menu_page( 'edit-comments.php' ); // Comments remove_menu_page( 'themes.php' ); // Appearance remove_menu_page( 'plugins.php' ); // Plugins remove_menu_page( 'users.php' ); // Users remove_menu_page( 'tools.php' ); // Tools remove_menu_page( 'options-general.php' ); // Settings } add_action( 'admin_menu', 'remove_admin_menus' );
// Remove sub level admin menus function remove_admin_submenus() { remove_submenu_page( 'index.php', 'update-core.php' ); // Dashboard - Updates remove_submenu_page( 'edit.php', 'post-new.php' ); // Posts - Add New Post remove_submenu_page( 'edit.php', 'edit-tags.php?taxonomy=category' ); // Posts - Categories remove_submenu_page( 'edit.php', 'edit-tags.php?taxonomy=post_tag' ); // Posts - Tags remove_submenu_page( 'edit.php?post_type=page', 'post-new.php?post_type=page' ); // Pages - Add New Page remove_submenu_page ( 'upload.php', 'media-new.php'); // Media - Add New remove_submenu_page( 'themes.php', 'themes.php' ); // Appearance - Themes //- Not working. Uses other code. See further below. remove_submenu_page( 'themes.php', 'customize.php' ); // Appearance - Customize remove_submenu_page( 'themes.php', 'widgets.php' ); // Appearance - Widgets remove_submenu_page( 'themes.php', 'nav-menus.php' ); // Appearance - Menus //- Not working remove_submenu_page( 'themes.php', 'customize.php' ); // Appearance - Header //- Not working remove_submenu_page( 'themes.php', 'customize.php' ); // Appearance - Background remove_submenu_page( 'themes.php', 'theme-editor.php' ); // Appearance - Editor remove_submenu_page( 'plugins.php', 'plugin-install.php' ); // Plugins - Add New Plugins remove_submenu_page( 'plugins.php', 'plugin-editor.php' ); // Plugins - Editor remove_submenu_page( 'users.php', 'user-new.php' ); //Users - Add New User remove_submenu_page( 'users.php', 'profile.php' ); //Users - Your Profile remove_submenu_page( 'options-general.php', 'options-writing.php' ); // Settings - Writing remove_submenu_page( 'options-general.php', 'options-reading.php' ); // Settings - Reading remove_submenu_page( 'options-general.php', 'options-discussion.php' ); // Settings - Discussion remove_submenu_page( 'options-general.php', 'options-media.php' ); // Settings - Media remove_submenu_page( 'options-general.php', 'options-permalink.php' ); // Settings - Permalink } add_action( 'admin_init', 'remove_admin_submenus' );
Removing Appearance -> Background and Header submenu pages require another function.
I found the code here: https://wordpress.org/support/topic/remove-header-and-background-option-pages-in-twenty-eleven-child-theme?replies=11
//Remove the custom options provided by the default twentyeleven theme. add_action( 'after_setup_theme','remove_header_background', 100 ); function remove_header_background() { remove_custom_background(); remove_custom_image_header(); }
RemovingAppearance -> Customize also requires a new function.
I found the code here: https://wordpress.org/support/topic/remove-customize-from-admin-menu?replies=5
function remove_customize_page(){ global $submenu; unset($submenu['themes.php'][6]); // remove customize link } add_action( 'admin_menu', 'remove_customize_page');
/* Add submenu item to another submenu - One big problem. The moved submenu link will open in th e original location. I used upload.php as an example on adding the media library. Check above in the remove_submenu_page for other options. */ function add_submenu_item() { // Resource page: https://clivern.com/adding-menus-and-submenus-for-wordpress-plugins/ // add_options_page($page_title, $menu_title, $capability, $menu_slug, $function); add_dashboard_page('page-title', 'submenu title', 'manage_options', 'upload.php'); add_posts_page('page-title', 'submenu title', 'manage_options', 'upload.php'); add_media_page('page-title', 'submenu title', 'manage_options', 'upload.php'); add_pages_page('page-title', 'submenu title', 'manage_options', 'upload.php'); add_comments_page('page-title', 'submenu title', 'manage_options', 'upload.php'); add_theme_page('page-title', 'submenu title', 'manage_options', 'upload.php'); add_plugins_page('page-title', 'submenu title', 'manage_options', 'upload.php'); add_users_page('page-title', 'submenu title', 'manage_options', 'upload.php'); //Tools add_management_page('page-title', 'submenu title', 'manage_options', 'upload.php'); //Settings add_options_page('page-title', 'submenu title', 'manage_options', 'upload.php'); } add_action( 'admin_init', 'add_submenu_item' );
// submenu item to top level menu // add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position ); // https://gist.github.com/hofmannsven/8269251 function new_menu() { add_menu_page('page-title','menu-title1','manage_options','widgets.php', '', 'dashicons-desktop');
// Submenu to new submenu -- It will still open in the original area. // add_submenu_page($parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function); add_submenu_page('widgets.php', 'page-title', 'menu-title2', 'manage_options', 'upload.php'); } add_action( 'admin_init', 'new_menu' );
I filled out three trac tickets. One for admin_menu not removing the theme editor 32784 the other for lack of consistency in removing submenu items 32785. The third when removing submenu item and then adding it to another existing top level menu clicking the moved submenu item will open it in the original location 32788.
I wrote a tutorial on adjusting the top admin tool bar menu.
Additional resources:
https://www.paulund.co.uk/add-menu-item-to-wordpress-admin
https://tommcfarlin.com/add-a-separator-to-the-wordpress-menu/
https://markwilkinson.me/2014/11/altering-wordpress-admin-menus/
https://codex.wordpress.org/Function_Reference/remove_menu_page
https://wordpress.stackexchange.com/questions/52099/how-to-remove-entire-admin-menu
Plugins:
https://wordpress.org/plugins/codepress-admin-columns/
https://wordpress.org/plugins/admin-menu-editor/
How do i move one menu item under another custom menu, for example, i want the tools menu to be placed under a custom menu that i have created.
I am able to move the menu as sub menu, but i am unable to fetch the sub menu of the menus that i moved.
Right now I do not remember/do not know.
If you can do some research and reply to your own comment the result that would be great.
Merry Christmas!
Hi Paal,
Great tutorial, very clear and easy to follow.
I’m using it to re-order the menu, but finding that some pages are not affected.
For example, Gravity Forms does not move.
Using the following format (going by the top level URL):
‘admin.php?page=gf_edit_forms’, // Gravity Forms
Another page for TablePress has the same (lack of) effect. That URL is:
/wp-admin/admin.php?page=tablepress
Any Ideas?
Thanks!
Paul
Hey Paul
I apologize for the late response.
Send Gravity Forms and TablePress support an e-mail and give as much detail as you can.
I am thinking they would be able to help you out.
If they give a response you believe would be good for me to add to this tutorial then please let me know.
Good luck.
Hi Paal Joachim :).
Great ressources !! Thanks for sharing 🙂 !
But I have one more question.
How can I remove, in WP multisite install, the sub-menus under “My websites” > “Name of the website” > “New post” and “Manage comment”, please 🙂 ?
TIA.
Amicably,
Pierre.
Hi Pierre
I my other tutorial on top admin tool bar adjustments one of the readers gave this link in the comments:
https://www.wp-tutorials.com/add-links-to-my-sites-sub-menus-log-out-media-links-pages-appearance-plugins-users-tools-and-settings/
Test it out and modify. I am looking at it myself as well and I am thinking I will add a custom version of the tutorial to my own Top Admin adjustments tutorial:
https://www.easywebdesigntutorials.com/adding-links-in-the-top-admin-toolbar-menu/
Have a great day!
Thanks, very helpful article with up-to-date info on how to edit WP’s admin.