How to Removing fields from the Media Uploader/Gallery on a Custom Post Type Edit Page

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;
}

Leave a Comment