Skip to main content
4 of 4
Second version of the code added. Duplicated parts removed from Version 1.
brasofilo
  • 22.1k
  • 8
  • 69
  • 264

Version 1

Deals with filtering based on Post ID

*From Computer* tab

we need to retrieve the $attachment_parent_id:

add_filter('attachment_fields_to_edit', 'wpse_53600_remove_media_upload_fields', 10000, 2);
function wpse_53600_remove_media_upload_fields( $form_fields, $post ) {
    $our_page_id = 4;
    $post_id = !empty( $_GET['post_id'] ) ? (int) $_GET['post_id'] : 0;
    $attachment_parent_id = !empty($post->post_parent) ? $post->post_parent : 0;
    
    if( $post_id == $our_page_id || $attachment_parent_id == $our_page_id ){
        // Please, check VERSION 2 for full code
    }
    return $form_fields;
}

*From URL* tab

looks like another approach is necessary:

add_filter('type_url_form_media', 'wpse_53600_remove_from_url_fields', 10, 1);
function wpse_53600_remove_from_url_fields($html) {
    $our_page_id = 4;
    $post_id = !empty( $_GET['post_id'] ) ? (int) $_GET['post_id'] : 0;

    if( $post_id != $our_page_id)
        return $html;
    
    // Used to hide the unwanted table rows
    $display_none = 'style="display:none"';

    $html = '';// Please, check VERSION 2 for full code

    return $html;
}


Version 2

Deals with filtering based on Post Type
/* * Remove extra fields from Media Upload tabs based on the Post Type * In this example, it is a CPT named 'movie', but it could be 'post' or 'page' * * http://wordpress.stackexchange.com/q/53600/12615 */ add_filter('type_url_form_media', 'wpse_53600_remove_from_url_fields', 10, 1); add_filter('attachment_fields_to_edit', 'wpse_53600_remove_media_upload_fields', 10000, 2); /* FILTERS THE 'FROM COMPUTER' TAB */ function wpse_53600_remove_from_url_fields($html) { $post_id = !empty( $_GET['post_id'] ) ? (int) $_GET['post_id'] : 0; $post_type = get_post_type($post_id); if( 'movie' != $post_type) return $html; // Used to hide the unwanted table rows $display_none = 'style="display:none"'; $html = Image     Audio, Video, or Other File

URL * Title * Link text, e.g. “Ransom Demands (PDF)”

Alternate Text Alt text for the image, e.g. “The Mona Lisa”

Image Caption

Alignment

None Left Center Right Link Image To:
None Link to image Enter a link URL or click above for presets.

HTML; return $html; } /* FILTERS THE REST OF THE TABS */ function wpse_53600_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); $attachment_parent_id = !empty($post->post_parent) ? $post->post_parent : 0; $attachment_post_type = get_post_type($attachment_parent_id); /* * This conditional will also filter the attachments in the Library tab that fall under the 'movie' CPT * Use the following to filter the Library tab: * * $library_tab = ( isset($_GET['tab']) && 'library' == $_GET['tab']) ? true : false; * */ if( 'movie' == $post_type || 'movie' == $attachment_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; }
brasofilo
  • 22.1k
  • 8
  • 69
  • 264