简体   繁体   中英

remove all user wordpress capabilities from a specific user

We want to remove specific user capability from a user, so we use that line of code below and it works for a specific capability:

                    $user->remove_cap( 'edit_pages');

but what if we want to delete all user capabilities of that specific user ?, we dont need to list all the capabilities because the line of code below doesn't work

                    $user->remove_cap( 'edit_pages', 'publish_products');

So is there is a way to delete all the capabilities with a line of code to remove all the capabilities instead of removing a specific capability ?

What you can do is get a list of all the capabilities said user has, then loop through them and remove each one like so:

$caps = $user->allcaps;

foreach($caps as $cap) {
    $user->remove_cap($cap);
}

I believe this should work, if it doesn't or you have any clarifications you'd like me to make please let me know.

Source for All Capabilities (allcaps)

Based on an earlier answer, you could loop through and remove them, but you'll need the name of the capability, not the value.

foreach($user->allcaps as $key=>$value) {
    $user->remove_cap($key);
}

I haven't tested this a lot, but simply setting the array of capabilities to an empty array should do it too.

$user->allcaps = array();

The WP_User::remove_all_caps() method does just that:

//Remove all of the capabilities of the user.
$user->remove_all_caps( );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM