Created
January 6, 2023 23:58
-
-
Save patrickfreitasdev/a5adda59e387e871a30e35e3bf8ed7e9 to your computer and use it in GitHub Desktop.
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 | |
//Based on https://thereforei.am/2011/03/15/how-to-allow-administrators-to-edit-users-in-a-wordpress-network/ | |
function mc_admin_users_caps( $caps, $cap, $user_id, $args ){ | |
foreach( $caps as $key => $capability ){ | |
if( $capability != 'do_not_allow' ) | |
continue; | |
switch( $cap ) { | |
case 'edit_user': | |
case 'edit_users': | |
$caps[$key] = 'edit_users'; | |
break; | |
case 'delete_user': | |
case 'delete_users': | |
$caps[$key] = 'delete_users'; | |
break; | |
case 'create_users': | |
$caps[$key] = $cap; | |
break; | |
} | |
} | |
return $caps; | |
} | |
add_filter( 'map_meta_cap', 'mc_admin_users_caps', 1, 4 ); | |
remove_all_filters( 'enable_edit_any_user_configuration' ); | |
add_filter( 'enable_edit_any_user_configuration', '__return_true'); | |
/** | |
* Checks that both the editing user and the user being edited are | |
* members of the blog and prevents the super admin being edited. | |
*/ | |
function mc_edit_permission_check() { | |
global $current_user; | |
$screen = get_current_screen(); | |
get_currentuserinfo(); | |
if( ! is_super_admin( $current_user->ID ) && in_array( $screen->base, array( 'user-edit', 'user-edit-network' ) ) ) { // editing a user profile | |
//get user ID from URL | |
$user_id = ( isset( $_GET['user_id'] ) ) ? $_GET['user_id'] : false; | |
if ( $user_id && is_super_admin( $user_id ) ) { // trying to edit a superadmin while less than a superadmin | |
wp_die( __( 'You do not have permission to edit this user.' ) ); | |
} elseif ( ! ( is_user_member_of_blog( $user_id, get_current_blog_id() ) && is_user_member_of_blog( $current_user->ID, get_current_blog_id() ) )) { // editing user and edited user aren't members of the same blog | |
wp_die( __( 'You do not have permission to edit this user.' ) ); | |
} | |
} | |
} | |
add_filter( 'admin_head', 'mc_edit_permission_check', 1, 4 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment