Skip to content

Instantly share code, notes, and snippets.

@spartie17
Last active April 6, 2018 01:28
Show Gist options
  • Save spartie17/690e6b806a4ca56ad219263672baf44f to your computer and use it in GitHub Desktop.
Save spartie17/690e6b806a4ca56ad219263672baf44f to your computer and use it in GitHub Desktop.
// GIVE additional capabilities to 'Manager'
function additional_role_caps( $caps ) {
$user = wp_get_current_user();
if ( in_array( 'manager', (array) $user->roles ) ) {
$caps[ 'edit_theme_options' ] = true;
$caps[ 'list_users' ] = true;
$caps[ 'create_users' ] = true;
$caps[ 'delete_users' ] = true;
$caps[ 'edit_users' ] = true;
$caps[ 'promote_users' ] = true;
}
return $caps;
}
add_filter( 'user_has_cap', 'additional_role_caps' );
// CREATE new role based on Editor
add_action('init', 'cloneUserRole');
function cloneUserRole() {
global $wp_roles;
if (!isset($wp_roles))
$wp_roles = new WP_Roles();
$auth = $wp_roles->get_role('editor');
$wp_roles->add_role('manager', 'Manager', $auth->capabilities);
}
remove_role( 'author' );
// REMOVE 'Administrator' from the list of roles if the current user is not an admin
function remove_admin( $roles ){
if( !current_user_can('administrator') ){
unset( $roles['administrator']);
unset( $roles['manager']);
}
return $roles;
}
add_action('editable_roles','remove_admin');
// REMOVE Admin user from user list
add_action('pre_user_query','isa_pre_user_query');
function isa_pre_user_query($user_search) {
$user = wp_get_current_user();
if ($user->ID != 1) { // Is not administrator, remove administrator
global $wpdb;
$user_search->query_where = str_replace('WHERE 1=1', "WHERE 1=1 AND {$wpdb->users}.ID<>1",$user_search->query_where);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment