[wp-hackers] Mapping meta caps only to attachments and unpublished posts

Andrew Nacin wp at andrewnacin.com
Thu Jan 13 18:14:57 UTC 2011


On Thu, Jan 13, 2011 at 1:00 PM, William Davis <will.davis at gmail.com> wrote:

> I'm trying to allow users to only delete unpublished posts and attachments.
> I removed their meta cap to delete posts and am trying to add it back
> selectively with the code below, but am not having any luck. Any help would
> be much appreciated.
>
> if ( 'delete_post' == $cap ) {
>                $post = get_post( $args[0] );
>                $post_type = $post->post_type;
>                if( $post_type == 'attachment' ) {
>                        $post_type = get_post_type_object( $post->post_type
> );
>                        $caps[] = 'delete_post';
>                }
> }
>

You need to map to the primitive cap via map_meta_cap. Thus, $caps[] =
'delete_posts' rather than 'delete_post'.

Keep in mind you're not actually modifying the user's capability here --
you're simply saying that the user needs to have delete_posts whenever
delete_post is checked for an attachment.

Nacin


More information about the wp-hackers mailing list