Skip to content

Instantly share code, notes, and snippets.

@mannieschumpert
Last active February 22, 2021 05:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mannieschumpert/8888351 to your computer and use it in GitHub Desktop.
Save mannieschumpert/8888351 to your computer and use it in GitHub Desktop.
Some filter examples for preventing editing of certain users.
<?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;
}
<?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