Expanding new Media Uploader in WordPress 3.5

You can add inputs and fields to attachments by hooking into attachment_fields_to_edit. This will add fields to both the modal as well as the attachment editor. The catch I found is that WordPress (if anyone has experienced differently PLMK) doesn’t save the extra fields data so you have to hook into a couple other items.

I have added a code sample to give you add idea of what you could do.

add_filter( 'attachment_fields_to_edit', 'xf_attachment_fields', 10, 2 );

function xf_attachment_fields( $fields, $post ) {

 $meta = get_post_meta($post->ID, 'meta_link', true);
 $fields['meta_link'] = array(
    'label' => 'More Media Management',
    'input' => 'text',
    'value' => $meta,
     // 'html' => '<div class="meta_link"><input type="text" /></div>',
   'show_in_edit' => true,
 );
 return $fields;
}
add_filter( 'attachment_fields_to_save', 'xa_update_attachment_meta', 4);

function xa_update_attachment_meta($attachment){
  global $post;
  update_post_meta($post->ID, 'meta_link', $attachment['attachments'][$post->ID]['meta_link']);
}

add_action('wp_ajax_save-attachment-compat', 'xa_media_xtra_fields', 0, 1);
function xa_media_xtra_fields() {
  $post_id = $_POST['id'];
  $meta = $_POST['attachments'][$post_id ]['meta_link'];
  update_post_meta($post_id , 'meta_link', $meta);
  clean_post_cache($post_id);
}

Leave a Comment