Make WordPress Core

Opened 12 years ago

Last modified 2 years ago

#19747 reopened defect (bug)

Roles add_cap should call update_user_level_from_caps()

Reported by: lgladdy's profile lgladdy Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.3.1
Component: Role/Capability Keywords:
Focuses: Cc:


Hey guys,

I've created a new user role, which didn't appear in the Author drop down list due to #16841. The workaround is to add a level_1 capability to that user, but because wordpress doesn't call update_user_level_from_caps() function when you add_cap, that isn't actually applied to existing users on the role, and you have to move them to a different role and back again if you want to apply the changed user role.

I'd suggest adding a call to update_user_level_from_caps() to add_cap (and remove_cap) to fix this in wp-includes/capabilities.php

Change History (11)

#1 @scottconnerly
12 years ago

  • Cc scott@… added

#2 @BandonRandon
12 years ago

  • Cc BandonRandon added

#3 @nacin
10 years ago

  • Keywords reporter-feedback needs-unit-tests added

Hi lgladdy, sorry you never received a reply here. I'm trying to figure out what's going on. update_user_level_from_caps() gets called when you add a role to a user, which is what you'd be doing when you create a new user role. So maybe the API could do a little more, but I'm not quite sure what the bug is.

#4 @lgladdy
10 years ago

Hey Nacin,

It's been a little while since i opened this, so i'm trying to recall our use case, I think it was basically that when you add a deprecated level_x capability to a role, any users who already have that role aren't updated with that capability, because update_user_level_from_caps() isn't called as part of the add_cap.

Looking back, this probably isn't too much of an issue depending on the state of #16841 - I've not needed to do anything with caps in recent releases, but this was back in 3.3 so it may well be something that's changed since then!

This ticket was mentioned in Slack in #core by lgladdy. View the logs.

9 years ago

#7 @lgladdy
9 years ago

  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #28374.

Reading the patch for #28374, that resolves this ticket :)

Last edited 9 years ago by lgladdy (previous) (diff)

#8 @SergeyBiryukov
9 years ago

  • Keywords reporter-feedback needs-unit-tests removed
  • Milestone Awaiting Review deleted

#9 @pikamander2
3 years ago

  • Resolution duplicate deleted
  • Status changed from closed to reopened

This appears to still be an issue, at least in 5.8.

I was facing the exact same problem as @lgladdy where my custom role wasn't receiving the capability that I added to it via add_cap().

His blog post ( mentioned changing their role to something else and back as a workaround, but instead of trying that I just deleted the user and recreated them, at which point they had the new capability.

@SergeyBiryukov @nacin - Is it possible that there was a regression here at some point?

I see that 5.9 is going to be fixing a semi-related longstanding issue (#16841), so I'd be curious to know if that happens to fix it on its own.

Last edited 2 years ago by SergeyBiryukov (previous) (diff)

#10 @pikamander2
3 years ago

(Sorry, I didn't mean to remove it as a duplicate)

I just tried @lgladdy's suggested workaround (changing their role to something else and back) on another affected account and that also fixed it, so it definitely seems like the original bug is still around.

For reference, I'm on WordPress 5.8.2.

#11 @SergeyBiryukov
2 years ago

  • Milestone set to Awaiting Review
Note: See TracTickets for help on using tickets.