Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Please update your question to reference the current stable release of WordPress 3.5, as RC2 was pre-release software. –  EAMann Dec 11 '12 at 18:46
add comment

2 Answers

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);
}
share|improve this answer
    
Thanks for this. I needed to add return $attachment; to the xa_update_attachment_meta callback, but after that, it worked. –  goto10 Jan 16 '13 at 15:48
2  
This thread seems abandoned by its OP, but I'm wondering if all this "compat" stuff is really where it's at in WP 3.5. Is there a new, current (as in "not backward compatible, but by design going forward") method to add this kind of thing in WP 3.5+? –  k3davis Jan 29 '13 at 1:24
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.