3

I'm using the code below to add a custom text field in WP 3.5 Attachment Window (from this question # Expanding new Media Uploader in WordPress 3.5) ...

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']);
    return $attachment;
}

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);
}

This code adds the field there and I can also save and afterwards retrieve value for any of my attachment. But the issue is this code does NOT saves value of any attachment by using AJAX.

Mean, if for any attachment image I define any value in this custom field, then selecting another image will remove my defined value for that image apparently. However, if I update the post and check again that field the saved value is there.

2
  • 1
    Multi-post: stackoverflow.com/q/14572444, please see: meta.stackexchange.com/q/64068
    – brasofilo
    Jan 28, 2013 at 22:44
  • You haven't left any feedback yet on this question. If the answer below solved your issue, mark it as accepted. Otherwise, post your own solution and accept it. Don't let this question get recycled by the system indefinitely. Thank you Jan 23, 2015 at 5:05

1 Answer 1

5

I wanted to be able to add author information to my attachments and merged this code: http://www.billerickson.net/wordpress-add-custom-fields-media-gallery/ with the one you refer to. I Got it to work fully in the modal window via AJAX. The modified code is as follows:

/**
 * Add Author Name and URL fields to media uploader
 *
 * @param $form_fields array, fields to include in attachment form
 * @param $post object, attachment record in database
 * @return $form_fields, modified form fields
 */
function admin_attachment_field_media_author_credit( $form_fields, $post ) {

    $form_fields['media-author-name'] = array(
        'label' => 'Author Name',
        'input' => 'text',
        'value' => get_post_meta( $post->ID, 'media_author_name', true )
        //'helps' => 'If provided, author credit will be displayed'
    );

    $form_fields['media-author-url'] = array(
        'label' => __('Author URL',b()),
        'input' => 'text',
        'value' => get_post_meta( $post->ID, 'media_author_url', true ) 
        //'helps' => 'If provided, the author credit will be linked'
    );

    return $form_fields;

} add_filter( 'attachment_fields_to_edit', 'admin_attachment_field_media_author_credit', 10, 2 );

/**
 * Save values of Author Name and URL in media uploader
 *
 * @param $post array, the post data for database
 * @param $attachment array, attachment fields from $_POST form
 * @return $post array, modified post data
 */

function admin_attachment_field_media_author_credit_save( $post, $attachment ) {

    if( isset( $attachment['media-author-name'] ) )
        update_post_meta( $post['ID'], 'media_author_name', $attachment['media-author-name'] );

    if( isset( $attachment['media-author-url'] ) )
        update_post_meta( $post['ID'], 'media_author_url', $attachment['media-author-url'] );

    return $post;

} add_filter( 'attachment_fields_to_save', 'admin_attachment_field_media_author_credit_save', 10, 2 );

/**
 * Save values of Author Name and URL in media uploader modal via AJAX
 */

function admin_attachment_field_media_author_credit_ajax_save() {

    $post_id = $_POST['id'];

    if( isset( $_POST['attachments'][$post_id]['media-author-name'] ) )
        update_post_meta( $post_id, 'media_author_name', $_POST['attachments'][$post_id]['media-author-name'] );

    if( isset( $_POST['attachments'][$post_id]['media-author-url'] ) )
        update_post_meta( $post_id, 'media_author_url', $_POST['attachments'][$post_id]['media-author-url'] );

    clean_post_cache($post_id);

} add_action('wp_ajax_save-attachment-compat', 'admin_attachment_field_media_author_credit_ajax_save', 0, 1); 

Hope this was helpful!

P.S. The next challenge will be to append this to images uploaded in the document. D.S.

1
  • not working for me. the gallerie does not stop loading in post edit view
    – user9
    Mar 17, 2016 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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