Adding Custom Capabilites

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:

wp_current_user()->add_cap()

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

Solutions Collecting From Web of "Adding Custom Capabilites"

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.