All WordPress snippets
Want your own copy?
Fork this repo and build your own snippet collection. Fork on GitHub →
WordPress
Hide Admin Menu Items for a Custom User Role
Clones the Administrator role into a new “Client” role, then trims the admin menu specifically for that role — gives clients full capability without a cluttered dashboard.
php
/**
* Clone the administrator user role
*/
function clone_admin_role() {
global $wp_roles;
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles();
$adm = $wp_roles->get_role( 'administrator' );
// Add new "Client" role with all admin capabilities
$wp_roles->add_role( 'client', 'Client', $adm->capabilities );
}
add_action( 'init', 'clone_admin_role' );
/**
* Specify which admin menu items are visible for users with role "Client"
*/
function remove_dashboard_menus() {
if ( current_user_can( 'client' ) ) {
// Hide Updates under Dashboard menu
remove_submenu_page( 'index.php', 'update-core.php' );
// Hide Comments
remove_menu_page( 'edit-comments.php' );
// Hide Plugins
remove_menu_page( 'plugins.php' );
// Hide Themes, Customizer and Widgets under Appearance menu
remove_submenu_page( 'themes.php', 'themes.php' );
remove_submenu_page( 'themes.php', 'customize.php?return=' . urlencode( $_SERVER['REQUEST_URI'] ) );
remove_submenu_page( 'themes.php', 'widgets.php' );
// Hide Tools
remove_menu_page( 'tools.php' );
// Hide General Settings
remove_menu_page( 'options-general.php' );
}
}
add_action( 'admin_menu', 'remove_dashboard_menus' );
More WordPress snippets
Register and Output a Custom Nav Menu
Adds a new menu location you can assign in Appearance → Menus, shows how to output it in a template, and includes the multi-menu version too.
Define a Reusable Global String
A simple pattern for storing a value — like a phone number or address — in one place and reusing it anywhere in your theme.
Auto-Escape HTML Typed Inside Code Tags
Makes sure any markup typed inside <code> or <tt> tags in the editor gets escaped automatically, so it displays as visible text instead of being rendered as real HTML.