Last active
February 22, 2021 05:02
-
-
Save mannieschumpert/8888351 to your computer and use it in GitHub Desktop.
Some filter examples for preventing editing of certain users.
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 | |
/** | |
* Prevent Editing of a specified user | |
* | |
* This example shows how you can protect the original admin from being edited or deleted by anyone else | |
*/ | |
add_filter('map_meta_cap', 'prevent_user_edit', 10, 4 ); | |
function prevent_user_edit( $required_caps, $cap, $user_id, $args ){ | |
$protected_user = 1; // ID of user not editable | |
if ( $user_id === $protected_user ) // Don't block caps if current user = protected user | |
return $required_caps; | |
$blocked_caps = array( | |
'delete_user', | |
'edit_user' | |
); | |
if ( in_array( $cap, $blocked_caps ) && $args[0] === $protected_user ) | |
$required_caps[] = 'do_not_allow'; | |
return $required_caps; | |
} |
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 | |
/** | |
* Prevent Editing of an array of users | |
*/ | |
add_filter('map_meta_cap', 'prevent_users_edit', 10, 4 ); | |
function prevent_users_edit( $required_caps, $cap, $user_id, $args ){ | |
$protected_users = array(1,4,19); // IDs of users not editable | |
$allowed_editor = 1; // ID of user who can edit | |
if ( $user_id === $allowed_editor ) // Don't block caps if allowed editor | |
return $required_caps; | |
$blocked_caps = array( | |
'delete_user', | |
'edit_user' | |
); | |
if ( in_array( $cap, $blocked_caps ) && in_array( $args[0], $protected_user ) ) | |
$required_caps[] = 'do_not_allow'; | |
return $required_caps; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment