Is it possible to restrict access to WordPress widgets area for selected role? “Widgets” menu item is located under “Appearance” menu at WordPress administrator back-end. WordPress uses the same “edit_theme_options” user capability to manage access to it as for the rest items of “Appearance” submenu (read more…). Thus if user has access to the “Widgets” menu, he has access to the rest menu items: “Themes”, “Customize”, “Menu”, “Header”, “Background”. What to do if you wish to allow some user change themes, customize their appearance, edit menu, but do not allow him access to the “Widgets” menu item?
We can achieve this with additional PHP code, used as the must-use plugin:
// block Widgets menu for users with role Editor
function remove_widgets_submenu() {
global $submenu;
if (!current_user_can('editor')) {
return;
}
// remove "Widgets" submenu
foreach($submenu['themes.php'] as $key=>$item) {
if ($item[2]=='widgets.php') {
unset($submenu['themes.php'][$key]);
break;
}
}
}
add_action('admin_head', 'remove_widgets_submenu');
function widgets_redirect() {
$result = stripos($_SERVER['REQUEST_URI'], 'widgets.php');
if ($result!==false) {
wp_redirect(get_option('siteurl') . '/wp-admin/index.php');
}
}
add_action('admin_menu', 'widgets_redirect');
Create folder “wp-content/mu-plugins”, place there file with code above. It will be executed automatically for every page request. “mu” means “must use”.
You may download this file also.
This example blocks “Widgets” menu for the users with modified role “Editor” – “edit_theme_options” capability was added. User role is checked at line #6. Just replace “editor” there to any role for which you wish to block access to the “Widgets” menu.