Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress Editor can add and delete users, but not Admin level (for functions.php)
<?php
// ADD USER PERMISIONS FOR EDITOR
function add_theme_caps() {
$role = get_role( 'editor' );
$role->add_cap( 'edit_users' );
$role->add_cap( 'delete_users' );
}
add_action( 'admin_init', 'add_theme_caps');
// EDITOR CAN'T DELETE ADMIN USERS
function editor_cant_delete_admin($user_search) {
$user = wp_get_current_user();
if ($user->ID!=1) {
global $wpdb;
$user_search->query_where = str_replace('WHERE 1=1',
"WHERE 1=1 AND {$wpdb->users}.ID<>1",$user_search->query_where);
}
}
add_action('pre_user_query','editor_cant_delete_admin');
// EDITOR CAN'T ADD ADMIN USERS
function editor_cant_add_admin( $editable_roles ) {
global $pagenow;
if ( 'user-new.php' == $pagenow ) {
unset( $editable_roles['administrator'] );
}
return $editable_roles;
}
add_filter( 'editable_roles', 'editor_cant_add_admin' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.