Add a metabox when editing a media – but only for images

gordie

Here is an example of meta box for bellow possible mime types
‘image/jpeg’,’image/gif’,’image/png’,’image/bmp’,’image/tiff’,’image/x-icon’

//add meta_box for image mime types only
function add_our_attachment_meta(){
    $attachment_id          = $_REQUEST['post'];
    $attachment_mime_type   = get_post_mime_type($attachment_id);
    $possible_mime_types    = array('image/jpeg','image/gif','image/png','image/bmp','image/tiff','image/x-icon');
    if(in_array($attachment_mime_type, $possible_mime_types)){
            add_meta_box( 'custom-attachment-meta-box', 
                            'Attachment Extra Details', 
                            'our_attachment_meta_box_callback',
                            'attachment',
                            'normal',
                            'low');
    }
}
add_action( 'admin_init', 'add_our_attachment_meta' );

//callback function of custom metabox
function our_attachment_meta_box_callback(){
     global $post; 
     $value = get_post_meta($post->ID, 'featured_photo', 1);
?>
      <p>This will render inside of the meta box.</p>
    <select name="featured_photo">
     <option value="0" <?php selected( 0, $value ); ?> >No </option>
     <option value="1" <?php selected( 1, $value ); ?> >Yes </option>
    </select>
     <?php
}

//save meta on save post
function save_our_attachment_meta(){
     global $post; 
     if( isset( $_POST['featured_photo'] ) ){
           update_post_meta( $post->ID, 'featured_photo', $_POST['featured_photo'] );
     }
}
add_action('edit_attachment', 'save_our_attachment_meta');

In case of any doubt comment back i’ll surely take a look on it.

Thank You,