Expanding new Media Uploader in WordPress 3.5

In current version of WordPress it is quite easy to expand the attachments fields using attachment_fields_to_edit filter, but in latest WordPress 3.5 RC2 it’s not working. We can expand it using custom meta fields, but using this requires editing each attachment in Media tb. I was wondering if it is possible to expand fields in Attachment details in modal box with Media Library http://i.imgur.com/GIsJj.png (sorry for a link but I cannot add image, not enough reputation ;))

Thank you for any help.

Related posts

Leave a Reply

2 comments

  1. 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);
    }
    
  2. You should still be able to use attachment_fields_to_edit, however those fields are also added to the modal window now. You can direct a field to only show up in the modal or only show up on the edit page by using the “show_in_edit” or “show_in_modal” parameters. Both are boolean and both default to true. Take a look at this trac ticket explaining the behavior: http://core.trac.wordpress.org/ticket/22759#comment:1

    You should be aware that they seem to now be prefixed with “compat-field-” in the class names.