[wp-hackers] quick way to add dozen of new capabilities to existing role?

Davit Barbakadze jayarjo at gmail.com
Tue Sep 7 14:45:11 UTC 2010


I've considered that, but potential number of queries kinda frightened
me away. Looking at the source I just made up something like this:

function add_caps_to($roles, $caps)
{
   $r = new WP_Roles;

   if (!is_array($roles))
       $roles = array($roles);

   foreach ($caps as $cap)
       foreach ($roles as $role)
           $r->roles[$role]['capabilities'][$cap] = 1;

   update_option( $r->role_key, $r->roles );
}

Potentially it can simultaneously assign array of capabilities to a
several roles at ones and then make just one query at the end.

I wonder if there could be a problem with this?


On Tue, Sep 7, 2010 at 2:26 PM, Andrew Nacin <wp at andrewnacin.com> wrote:
> On Tue, Sep 7, 2010 at 5:54 AM, Davit Barbakadze <jayarjo at gmail.com> wrote:
>
>> Plugin creates two new custom post types with custom capabilities, is
>> there a quick way to assign these capabilities to existing roles -
>> administrator, editor?
>
>
> Something like this:
>
> $capabilities = array( ... );
> foreach ( array( 'administrator', 'editor' ) as $role ) {
>   $role =& get_role( $role );
>   if ( ! $role )
>       continue;
>   foreach ( $capabilities as $cap )
>      $role->add_cap( $cap );
> }
>
> Caution, that probably triggers a boat-load of queries. add_cap() should
> probably accept an array of capabilities to make that less taxing. (It only
> runs on activation, though.)
>
> Keep in mind you still need to handle meta capabilities yourself.
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>



-- 
Davit Barbakadze


More information about the wp-hackers mailing list