WordPress.org

Ready to get started?Download WordPress

Forums

I can't remove capability from specific user (4 posts)

  1. aznaddict
    Member
    Posted 3 years ago #

    I've been having trouble removing 'publish posts' capability from a specific user.

    ...
    if ($violations >= $bans){
    
    		$user = new WP_User($userid);
    		$user->remove_cap('publish_posts');
    }
  2. aznaddict
    Member
    Posted 3 years ago #

    This is broke. Try it out yourself.

    specifically, the remove_cap, add_cap to WP_User ability.
    If you follow this it will still not work.

  3. aznaddict
    Member
    Posted 3 years ago #

    I'm giving up at this point, I wrote a little more of the summary of the problem and posted the problem plugin here if anyone can solve the problem.

  4. itinerant
    Member
    Posted 3 years ago #

    I wanted to remove a capability from a user (leaving it in the role) as well and after a lot of experimenting and browsing through the WP core I have found this works:

    $user->add_cap("publish_posts",false)

    What happens is that $user->add_cap() puts an entry into the user capability overrides and $user->remove_cap(), deletes an entry from the overrides. To actually remove a capability the user inherits from their role(s) you have to use add_cap with a grant value of false. Basically the function names would be better called add_override and remove_override.

    If you find that still isn't producing the result you want, it may be that you need to be using get_user_to_edit($id) to generate your user object.

    This works in 3.1.4, I haven't tried out 3.2 yet.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.