Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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')?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|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. –  t f Mar 20 '13 at 20:15
    
Well, I meant has_cap, of course. :) –  t f 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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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