Last active
June 16, 2016 02:38
-
-
Save antcms/58880b6dc2cf01b469f080f58628b5a0 to your computer and use it in GitHub Desktop.
WordPress Editor can add and delete users, but not Admin level (for functions.php)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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