Customise the WordPress admin menu: remove, add and restrict items
The admin menu is built in PHP, and every item can be removed or added on the admin_menu hook. On client sites this is a real help: a shop manager who sees only Orders, Products and Pages makes fewer mistakes and needs less support than one who sees thirty menu items, half of which can break the site.
Problem: The client has an administrator account (they insist on it), and the menu shows tools they should not use: Plugins, Tools, the theme editor, the permalink settings.
Solution: Remove items with remove_menu_page() and remove_submenu_page() on admin_menu for every user except the developers, add your own pages with add_menu_page(), and block the hidden screens themselves, because hiding a link does not block the URL.
Removing items
The default roles already hide most things from non-administrators: editors do not see Plugins or Settings at all. The usual problem is a client with full rights, so the example decides by the user, not by the role:
<?php
function ha_is_developer() {
$user = wp_get_current_user();
return in_array( $user->user_login, array( 'dev-anna', 'dev-max' ), true );
}
add_action( 'admin_menu', 'ha_trim_admin_menu', 999 );
function ha_trim_admin_menu() {
if ( ha_is_developer() ) {
return;
}
remove_menu_page( 'plugins.php' );
remove_menu_page( 'tools.php' );
remove_menu_page( 'edit-comments.php' );
remove_submenu_page( 'themes.php', 'theme-editor.php' );
remove_submenu_page( 'options-general.php', 'options-permalink.php' );
}
The first argument is the menu slug, which for core pages is the file name from the URL. Priority 999 runs after plugins have added their menus, so their items can be removed too.
Blocking the screens
A removed item is only a missing link: the page still opens when the URL is typed. Block the screens as well:
<?php
add_action( 'current_screen', function ( $screen ) {
$blocked = array( 'plugins', 'tools', 'theme-editor', 'options-permalink' );
if ( ! ha_is_developer() && in_array( $screen->id, $blocked, true ) ) {
wp_die( esc_html__( 'This screen is not available.', 'textdomain' ), 403 );
}
} );
// No file editors for anybody (wp-config.php)
// define( 'DISALLOW_FILE_EDIT', true );
For real restrictions, a role with fewer capabilities is better than a trimmed menu: an “editor” or a custom “shop manager” role cannot install plugins even through direct URLs or the REST API. Trimming the menu is about convenience; capabilities are the security.
Adding your own pages
<?php
add_action( 'admin_menu', function () {
add_menu_page(
__( 'Site reports', 'textdomain' ), // page title
__( 'Reports', 'textdomain' ), // menu title
'edit_posts', // capability
'ha-reports', // slug
'ha_render_reports_page', // callback
'dashicons-chart-bar', // icon
25 // position (after Comments)
);
add_submenu_page( 'ha-reports', __( 'Traffic', 'textdomain' ), __( 'Traffic', 'textdomain' ), 'edit_posts', 'ha-reports-traffic', 'ha_render_traffic_page' );
} );
function ha_render_reports_page() {
echo '<div class="wrap"><h1>' . esc_html__( 'Site reports', 'textdomain' ) . '</h1></div>';
}
The admin bar has its own items that can be trimmed in a similar way, see hiding and extending the admin bar.
NOTE: Do not decide by a user ID or login in plugins you distribute; that works only on your own client sites. A plugin should check capabilities, and leave it to the site owner which users have them.