Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Created June 15, 2020 17:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/106e7c645cca10db6798b42931c4e167 to your computer and use it in GitHub Desktop.
Save damiencarbery/106e7c645cca10db6798b42931c4e167 to your computer and use it in GitHub Desktop.
Add capability to a specific user - Add 'manage_options' capability to an non-admin user. https://www.damiencarbery.com/2020/06/add-capability-to-a-specific-user/
<?php
/*
Plugin Name: Add capability to a specific user
Plugin URI: https://www.damiencarbery.com/2020/06/add-capability-to-a-specific-user/
Description: Add 'manage_options' capability to an non-admin user.
Author: Damien Carbery
Author URI: https://www.damiencarbery.com
Version: 0.1
*/
function dcwd_cap_filter( $allcaps, $cap, $args ) {
//error_log( 'AllCaps: ' . var_export( $allcaps, true ) );
//error_log( 'Capability: ' . var_export( $cap, true ) );
//error_log( 'Args: ' . var_export( $args, true ) );
$user_to_escalate = 4; // ID of the user to add the capability to.
$capability_to_check = 'manage_options'; // Can change to 'activate_plugins' etc.
if ( $capability_to_check == $cap[ 0 ] ) {
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
if ( $user_to_escalate == $user->ID ) {
$allcaps[ $capability_to_check ] = true;
}
}
}
return $allcaps;
}
add_filter( 'user_has_cap', 'dcwd_cap_filter', 10, 3 );
// Don't use this "permanent change" version.
/*function dcwd_add_manage_options_capability() {
$user_to_escalate = 4;
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
if ( $user_to_escalate == $user->ID ) {
$user->add_cap( 'manage_options');
}
return;
}
}
//add_action( 'admin_init', 'dcwd_add_manage_options_capability' );
add_action( 'init', 'dcwd_add_manage_options_capability' );
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment