Skip to content

Instantly share code, notes, and snippets.

@AlphaAlec
Created March 12, 2014 20:04
Show Gist options
  • Save AlphaAlec/9515182 to your computer and use it in GitHub Desktop.
Save AlphaAlec/9515182 to your computer and use it in GitHub Desktop.
Restricting Capabilities in the WP Back End
function change_role_name() {
global $wp_roles;
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles();
//You can list all currently available roles like this...
//$roles = $wp_roles->get_names();
//print_r($roles);
//You can replace "administrator" with any other role "editor", "author", "contributor" or "subscriber"...
$wp_roles->roles['administrator']['name'] = 'Special Admin';
$wp_roles->role_names['administrator'] = 'Special Admin';
}
add_action('init', 'change_role_name');
//Add a new role
add_action('init', 'cloneRole');
function cloneRole()
{
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('new_role', 'Administrator', $adm->capabilities);
}
function remove_menus () {
if(is_user_logged_in() && current_user_can('new_role')) {
global $menu;
//$restricted = array(__('Dashboard'), __('Posts'), __('Media'), __('Contact'), __('Links'), __('Pages'), __('Appearance'), __('Tools'), __('Users'), __('Settings'), __('Comments'), __('Plugins'));
$restricted = array(__('Comments'), __('Plugins'), __('Tools'), __('Custom Fields'), __('Themes'));
end ($menu);
while (prev($menu)){
$value = explode(' ',$menu[key($menu)][0]);
if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
}
}
}
add_action('admin_menu', 'remove_menus');
function remove_acf() {
if(is_user_logged_in() && current_user_can('new_role')) {
remove_menu_page('edit.php?post_type=acf');
remove_submenu_page( 'themes.php','theme-editor.php');
remove_submenu_page( 'themes.php','customize.php');
remove_submenu_page( 'themes.php','custom-background');
global $submenu;
unset($submenu['index.php'][10]);
return $submenu;
}
}
add_action('admin_menu','remove_acf',999);
function remove_admin_bar_links() {
if(is_user_logged_in() && current_user_can('new_role')) {
global $wp_admin_bar;
$wp_admin_bar->remove_menu('updates');
}
}
add_action( 'wp_before_admin_bar_render', 'remove_admin_bar_links' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment