Give Extra Capabilities to Specific Users
Grants chosen capabilities to individual user accounts, for example letting one editor manage plugins, without creating a new role.
Opens WPIDE on
Code
// User ID => capabilities to add.
$wpide_user_extra_caps = array(
2 => array( 'edit_theme_options', 'list_users' ),
);
add_filter( 'user_has_cap', function ( $allcaps, $caps, $args, $user ) use ( $wpide_user_extra_caps ) {
if ( empty( $wpide_user_extra_caps[ $user->ID ] ) ) {
return $allcaps;
}
foreach ( $wpide_user_extra_caps[ $user->ID ] as $cap ) {
$allcaps[ $cap ] = true;
}
return $allcaps;
}, 10, 4 );
Notes
Use user IDs, which never change. The capabilities are granted in memory and disappear when the snippet is off.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.