WordPress clone admin user role

Reorder left admin menu and add a custom user role

Clone the administrator user role.
Reorder the left WP admin menu.

Clone the administrator user role

When we need a new administrator user role. Create a new role by adding the following code to the child theme functions.php file or a code snippet plugin.

Even though the code is old it still seems to work.

/* https://www.kvcodes.com/2015/04/wordpress-clone-user-role-to-create-new-role/
https://nazmulahsan.me/clone-or-add-new-user-roles/ Duplicate the admin user role. */
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);

The result is seen when going to Users -> Add New. Click the Role drop down and notice the new Client admin user role.

WordPress clone admin user role
WordPress clone admin user role.

Modify the WordPress admin for the newly cloned user role.

Remove all top level links except Posts.
Remove submenu links such as Dashboard – Updates, Post tags, Appearance – Editor and Plugins – Editor.

/* 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 top level links.
 // 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	
 // Remove submenu links.
 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

The result of using the above code.

Modifying a new user role in WordPress
Modifying a new user role in WordPress.

All top level items are hidden except Posts. Dashboard Home is also seen.

Reorder 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 all top level and all submenu items.

/* Remove top level 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

// Posts
   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

// Appearance
   remove_submenu_page( 'themes.php', 'themes.php' ); // Appearance - Themes
   remove_submenu_page( 'themes.php', 'theme-editor.php' ); // Appearance - Editor 
remove_submenu_page( 'themes.php', 'widgets.php' ); // Appearance - Widgets
remove_submenu_page( 'themes.php', 'nav-menus.php' ); // Appearance - Menus
// Plugins
   remove_submenu_page( 'plugins.php', 'plugin-install.php' ); // Plugins - Add New Plugins
   remove_submenu_page( 'plugins.php', 'plugins.php' ); // Plugins - Installed Plugins	
   remove_submenu_page( 'plugins.php', 'plugin-editor.php' ); // Plugins - Editor

// Users
   remove_submenu_page( 'users.php', 'user-new.php' ); //Users - Add New User
   remove_submenu_page( 'users.php', 'profile.php' );  //Users - Your Profile

// Tools submenu links can also be removed. Hover over a link and notice the status bar link in the bottom left. Add the name similar to the other options seen in this code.

// Settings
   remove_submenu_page( 'options-general.php', 'options-general.php' ); // Settings - General	
   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

// Not working. Uses other code. See further below.
   remove_submenu_page( 'themes.php', 'customize.php' ); // Appearance - Customize

add_action( 'admin_init', 'remove_admin_submenus' );

RemovingAppearance -> Customize also requires a new function.
I found the code here: https://wordpress.org/support/topic/remove-customize-from-admin-menu?replies=5

/* Removes customize menu link. */
function remove_customize_page(){
 global $submenu;
 unset($submenu['themes.php'][6]); // remove customize link
add_action( 'admin_menu', 'remove_customize_page');

Add a submenu to another submenu.

The following is code to add a submenu to another submenu. If it uses an existing submenu it will still open in its original location.

/* Add submenu item to another submenu - The moved submenu link will open in the 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');
 add_management_page('page-title', 'submenu title', 'manage_options', 'upload.php');
 add_options_page('page-title', 'submenu title', 'manage_options', 'upload.php'); 
add_action( 'admin_init', 'add_submenu_item' );

Add submenu link to a top level menu.

/* submenu item to top level menu 
Example: add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position ); 
 https://gist.github.com/hofmannsven/8269251 */

add_action( 'admin_menu', 'register_my_custom_menu_page' );
function register_my_custom_menu_page() {
  add_menu_page( 'Custom Menu Page Title', 'Custom Menu Page', 'manage_options', 'custom.php', '', 'dashicons-awards', 90 );

The above code links to a new custom.php page which currently does not exist.

// Submenu to new submenu - It will still open in the original area.
Example: 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' );

Resources used:


Additional resources:





History: 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.

This tutorial was originally created 22 June 2015.


Leave a Reply

Your email address will not be published. Required fields are marked *