Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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