Support » Themes and Templates » current_user_can not working with custom roles

  • I’ve looked all over for this but it looks like if you’ve created a new role with add_role() and added appropriate capabilities, WordPress is not honoring those capabilities.

    when I dump the contents of the current_user array, I can clearly see that the capabilities for a custom role are in the right place but running the function current_user_can() for say, ‘edit_posts’ when that capability exists returns false.

    the add_role() function was added to my functions.php file and I can add users to it and see them reflected on the backend. The users can login and read content just fine but when it comes to doing anything more WordPress fails.

    Anyone out there dealt with this?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Which version of WordPress are you using? I think I’ve discovered a problem like this with user capabilities that is in 3.1. It had me stumped for a while and then I tried using 3.0.4 again and everything worked fine.

    Worth a try if you’re on the development version.

    I have used a plugin for creating and editing user roles, and I also cannot seem to get WP to honor the capabilities I am assigning to the custom roles. I am using WP 3.0.5 ….

    hey pabline — i am using 3.0 and simply cannot get it to work. did you simply use the add_role() function to make it work?

    DougJoseph — can’t help you there. probably best to ask the plugin developer directly.

    Checking against a custom capability with:
    if ( current_user_can( ‘CUSTOM_CAPABILITY’ ) )
    does not work here in WP 3.0.1 neither.
    add_role seems to write the name of the ROLE to the wp_capabilities fields in wp_usermeta, not the name of the custom CAPABILITY.
    If I check against the ROLE:
    if ( current_user_can( ‘ROLE’ ) )
    it does work, but that’s not what current_user_can is intended to do.

    Has anyone found an answer for Havahula’s and Geert’s problem?

    I’m guessing not. I don’t see anything on trac, so I’m guessing nobody has submitted it as a bug. Should we?

    I’m trying to assign Administrator custom capabilities and find that I cannot. Do I need to create a custom role, assign it the all of the same capabilities as Administrator PLUS my custom capabilities just to test these new capabilities with current_user_can()?

    I’d rather not. Why can’t we test custom capabilities with current_user_can? We need a new function register_capability() or something.

    hi everyone —

    just to be clear, the problem i was experiencing with 3.0 was the assignment of standard WordPress capabilities to custom roles using the add_role() function. Mainly, I was wanting to use this to allow for editing of custom content types through a front-end user interface.

    that said, the problem seems to have resolved with 3.1.1 — I just verified that by creating two custom roles; one with the ability to ‘upload_files’ the other did not. testing against that with current_user_can() generated the expected results. this worked simply by adding the desired capability to the array in add_role().

    HOWEVER, before this worked, what i ended up discovering is that if i first added the custom role with add_role and then re-assigned the desired capabilities by calling the global $wp_roles and then $wp_roles->add_cap( 'custom_role', 'desired_capability' ) i had some unexpected successes.

    so, for all of you who are creating your custom capabilities and assigning them to custom or standard roles, you might want to try that double-bump and see if it works. i’ve found some redundancies in WordPress core functions that have made me pause and, while i’m sure they’re there for a reason i’m also sure that it’s small quirks like that that allows hacks like the one i discovered to also work.

    good luck!

    @havahula, we tried your “double-bump” technique and it worked famously! Thanks for sharing, it also helped others in a discussion thread on our site.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘current_user_can not working with custom roles’ is closed to new replies.