So, I'm not sure if this is possible, but is it possible to add custom Capabilities?

So, I've written a group message application, that ties in with Wordpress. I only want certain members (set via options that I just asked about) to be able to send out such messages. I'd like to add a capability for, say "group_message", and add it to specific users (since it isn't strictly editors / administrators who have access to send out).

I know about:


But is the only thing I need to do, say: wp_current_user()->add_cap('group_message')?


I just happened to provide a short example of setting up a custom role capability (exaplanation and code). In your case, however, you want to add the capability to particular users - not roles.

The following code may be a starting point for what you want to do:

>>> Setting it up
// The IDs of the `privileged users`
$users = array( ... );

// Required arguments for the user query
$args = array(
    'include' => $users,
    'fields' => 'all_with_meta',

// Add the capability to privileged users
foreach ( get_users( $args ) as $user )
    $user->add_cap( 'group_message' );
>>> Using it
// Check for the capability
if ( current_user_can( 'group_message') ) {
    // group message stuff
>>> Cleaning it up
// Remove the capability
foreach ( get_users( 'fields' => 'all_with_meta' ) as $user )
    $user->remove_cap( 'group_message' );

All you need to do now, is think about getting/providing the user IDs. You could, for instance, list all users on your plugin menu page (if you have one), then select the ones you'd like to be capable of group messaging, and finally add the capability to those users.

| improve this answer | |
  • Sorry its taken me so long to respond, but thank you! I already have code in place that fetches those who need the access and plan on managing it. Will remove_cap break anything if I just cycle through all users and run it? – Jeremy Dentel Mar 20 '13 at 14:30
  • 1
    No, there's no problem doing remove_cap on all users. In fact, it is no more than an encapsulated unset (which does neither break nor return an error/warning). Of course, you could check by means of isset beforehand. – tfrommen Mar 20 '13 at 20:15
  • Well, I meant has_cap, of course. :) – tfrommen Mar 20 '13 at 20:24
  • 1
    Much appreciated for the help! Makes managing my system a hell of a lot easier. ;) – Jeremy Dentel Mar 21 '13 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.