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.

I have a CPT to replace my "post"-post-type, but I still need to be able to use attachments. The problem is, that attachments have the post-capabilities.

I don’t want to give post capabilities to my role, because of that, I tried to find a way to avoid it.

I found a hack in the wordpress track: http://core.trac.wordpress.org/ticket/19834

function crunchhack() {
   global $wp_post_types;
   $wp_post_types['attachment']->cap->edit_post = 'upload_files';
}
add_action( 'init', 'crunchhack' );

I have added it to my functions.php. It did not work, because of that, I tried to replace all post-capabilities of the attachment post-type with upload_files:

function fix_media_permissions() {
    global $wp_post_types;
    $wp_post_types['attachment']->cap->edit_post = 'upload_files';
    $wp_post_types['attachment']->cap->read_post = 'upload_files';
    $wp_post_types['attachment']->cap->delete_post = 'upload_files';
    $wp_post_types['attachment']->cap->edit_posts = 'upload_files';
    $wp_post_types['attachment']->cap->edit_others_posts = 'upload_files';
    $wp_post_types['attachment']->cap->edit_published_posts = 'upload_files';
    $wp_post_types['attachment']->cap->publish_posts = 'upload_files';
    $wp_post_types['attachment']->cap->delete_posts = 'upload_files';
    $wp_post_types['attachment']->cap->delete_others_posts = 'upload_files';
    $wp_post_types['attachment']->cap->delete_published_posts = 'upload_files';
    $wp_post_types['attachment']->cap->delete_private_posts = 'upload_files';
    $wp_post_types['attachment']->cap->edit_private_posts = 'upload_files';
    $wp_post_types['attachment']->cap->read_private_posts = 'upload_files';
}
add_action( 'init',  'fix_media_permissions');

Sadly this doesn’t work either. I can see the uploaded images, but that’s it. Any ideas?

share|improve this question
1  
Im curious, why did you replace the perfectly good posts post type with a whole new post type? Was there something that prevented you using multisite or custom taxonomies? –  Tom J Nowell Mar 30 '13 at 2:53
    
I needed a post type with a custom meta box with a list that handles the attachments of this post. Looking back now, it might have been easier to try to alter the posts post type, but it seamed easier to create a new post type for it. –  Afterlame Mar 30 '13 at 3:00
1  
It is pretty easy, almost trivial, to add meta_boxes. –  s_ha_dum Mar 30 '13 at 3:14
    
There are plugins that will do the work for you even, for the most part its a copy paste affair out of the codex –  Tom J Nowell Mar 30 '13 at 3:31
    
The problem in altering the "post" post-type is, that if I want to use it in addition to the new post-type, altering the original is no option. Creating the post-type never was a problem, I struggled with the permissions. I didn’t want to use plugins, because I am trying to understand how things work in Wordpress. Anyway, I solved it and everything works like it should. –  Afterlame Mar 30 '13 at 12:14

1 Answer 1

up vote 1 down vote accepted

I solved it. The problem was, that I called the function inside of another function, called later. The snippet in the question works like it should.

I thought about deleting the question, but I think for other users, that are searching for a way to do something like that, the snippet would be interesting.

share|improve this answer

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.