WordPress.org

Ready to get started?Download WordPress

Forums

Individual capabilities without role (3 posts)

  1. chrismichaels84
    Member
    Posted 7 months ago #

    I feel like this should be fairly simple, but I can't figure out where to start. I want to create new capabilities such as "add_wiki_post" and "invite_others" that are linked to certain custom made plugins for my site. That way, I can use current_user_can("add_wiki_post") and all will be peachy. My issue is that I don't want these capabilities to be assigned to a specific role. I want them to be free floating so I can assign them to users individual. Thus one contributor may have "add_wiki_post" but another may not.

    Is this possible with capabilities, or would it be better to create my own set of usermeta and kinda make a second caps system?

    Thanks! I'd love some creative solutions. And, I will release this code as a plugin. I'm adding capabilities to badges.

  2. bcworkz
    Member
    Posted 7 months ago #

    Yes, you can have capabilities independent of roles. If you have the user_id, just do this:

    $user = new WP_User( $user_id );
    $user->add_cap( 'add_wiki_post' );

    You then could see the capability added to the 'wp_capabilities' usermeta along with the user's role and if the user were to log in, the function current_user_can('add_wiki_post') would return true.

    The problem is the admin screens are setup to only manage users by role. You'll more than likely need a custom section on the user profile screen where you can easily assign or remove capabilities independent of roles. This is certainly doable, but it'll take more than 2 lines :)

  3. chrismichaels84
    Member
    Posted 7 months ago #

    That is perfect. I'm attaching capabilities to earned badges and have the system set up to automatically assign the badges. I can attach the capabilities to that action easily enough.

    Cheers!

Reply

You must log in to post.

About this Topic