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.

This post shows how to remove unwanted fields from the media uploader. It works great, but I want to limit it to a custom-post-type and I can't seem to figure out how to do it. I've tried the following code, but it doesn't seem to work:

add_filter('attachment_fields_to_edit', 'remove_media_upload_fields', 10000, 2);
function remove_media_upload_fields( $form_fields, $post ) {
global $my_admin_page;
$screen = get_current_screen();
if ( 'slider' == $screen->post_type ){

unset( $form_fields['image-size'] );
unset( $form_fields['post_excerpt'] );
unset( $form_fields['url'] );
unset( $form_fields['image_url'] );
unset( $form_fields['align'] );

}

return $form_fields;
}

Any ideas on how to get this to work?

Thanks,

Moshe

share|improve this question
    
Please update your question to explain what fields you want to remove from a custom post type, how that post type is registered, and anything else you've tried. I get the feeling that the code snippet you included isn't relevant, but maybe I just don't understand the question. –  mrwweb May 9 '12 at 3:17

1 Answer 1

The current screen doesn't appear to be set when that filter is run, so you cannot use that. Also, the $post actually refers to the attachment, not the post - so we can't get the post typ fro that either....

So looking at the source code: http://core.trac.wordpress.org/browser/tags/3.3.2/wp-admin/includes/media.php

The filter you are using is called by get_attachment_fields_to_edit, tracing this - we find its called by get_media_item.

Looking at get_media_time, it unfortunately does not pass the post ID or object. However, it does have:

$current_post_id = !empty( $_GET['post_id'] ) ? (int) $_GET['post_id'] : 0;

This indicates that the only way of getting the post ID (and thus post type), is to grab it from $_GET. It would be nicer if this was passed as along with the $args array that is defined then.

So:

add_filter('attachment_fields_to_edit', 'remove_media_upload_fields', 10000, 2);
function remove_media_upload_fields( $form_fields, $post ) {
    $post_id = !empty( $_GET['post_id'] ) ? (int) $_GET['post_id'] : 0;
    $post_type = get_post_type($post_id);

    if( 'slider' == $post_type ){
        // remove unnecessary fields
        unset( $form_fields['image-size'] );
        unset( $form_fields['post_excerpt'] );
        unset( $form_fields['post_content'] );
        unset( $form_fields['url'] );
        unset( $form_fields['image_url'] );
        unset( $form_fields['align'] );
    }
    return $form_fields;
}
share|improve this answer
    
Just wanted to say thank-you - this worked exactly as I wanted. –  user15927 May 20 '12 at 12:50

Your Answer

 
discard

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