dustbro
dustbro

Reputation: 19

Add/Remove Capabilities for logged in user

I need some help dynamically assigning capabilities to users when they log in. Specifically, I need the users to have "list_users" capabilities.

I believe I can use the following code to grant them "list_users", but how do I remove it when they are logged out?

// get user ID
$user_id = get_current_user_id();

// add the capability to the specific user
$user_id->add_cap("list_users" => true);

EDIT: This code above crashes my site

Upvotes: 0

Views: 157

Answers (1)

Md. Delowar Hossen
Md. Delowar Hossen

Reputation: 561

add/remove capability to/from specific user -

// get user ID
$user_id = get_current_user_id();

//to remove capability from user
$user = new WP_User( $user_id );
$user->remove_cap( 'can_email');

//to add capability to user
$user = new WP_User( $user_id );
$user->add_cap( 'can_email');


//to add capability to list_users
$user = new WP_User( $user_id );
$user->add_cap( 'list_users');

Upvotes: 1

Related Questions