Skip to content

Instantly share code, notes, and snippets.

@krasenslavov
Last active December 26, 2021 00:32
Show Gist options
  • Save krasenslavov/181c7814c43cafbe86571cc6edf84898 to your computer and use it in GitHub Desktop.
Save krasenslavov/181c7814c43cafbe86571cc6edf84898 to your computer and use it in GitHub Desktop.
Extend User Profiles and Restrict Access to a Single Admin Only
<?php
if (!class_exists('Admin_User_Caps')) {
class Admin_User_Caps
{
function __construct()
{
add_action('admin_init', array($this, 'extend_user_profile'));
}
public function extend_user_profile()
{
// Add field for user access to the Plugin.
add_action('user_new_form', array($this, 'register_profile_fields')); // /wp-admin/user-new.php
add_action('edit_user_profile', array($this, 'register_profile_fields')); // /wp-admin/user-edit.php?user_id=1
// add_action('show_user_profile', array($this, 'register_profile_fields')); // /wp-admin/profile.php
// Save user access to the Plugin as usermeta.
add_action('user_register', array($this, 'save_profile_fields')); // /wp-admin/user-new.php
add_action('edit_user_profile_update', array($this, 'save_profile_fields')); // /wp-admin/user-edit.php?user_id=1
// add_action('personal_options_update', 'cp_save_profile_fields'); // /wp-admin/profile.php
}
public function register_profile_fields($user)
{
/**
* We want to restrict Administrators to change the BKPC access themselves.
*
* If Admin use is created without access to the Plugin then they won't
* be able to Add, Edit or Update others with this options.
*/
$current_user_id = get_current_user_id();
$current_user_can = get_user_meta($current_user_id, 'user_can_access_plugin_name')[0];
$update_user_can = get_user_meta($user->ID, 'user_can_access_plugin_name')[0];
if (
!current_user_can('administrator', $current_user_id)
|| $current_user_can === null) {
return false;
}
?>
<h2><br />Extend User Options</h2>
<table class="form-table">
<tr>
<th>
<label for="user_can_access_plugin_name">User can have access<br /> to Plugin?</label>
</th>
<td>
<?php if ($update_user_can !== null) : ?>
<input type="checkbox" class="regular-text" name="user_can_access_plugin_name" value="1" id="user_can_access_plugin_name" checked />
<?php else : ?>
<input type="checkbox" class="regular-text" name="user_can_access_plugin_name" value="1" id="user_can_access_plugin_name" />
<?php endif; ?>
<em>This is useful when you want to restrict access to Plugin for other Administrators.</em>
</td>
</tr>
</table>
<?php
}
public function save_profile_fields($update_user_id)
{
$current_user_id = get_current_user_id();
$current_user_can = get_user_meta($current_user_id, 'user_can_access_plugin_name')[0];
if (
!current_user_can('administrator', $current_user_id)
|| $current_user_can === null) {
return false;
}
update_user_meta($update_user_id, 'user_can_access_plugin_name', $_POST['user_can_access_plugin_name']);
}
}
new Admin_User_Caps;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment