WordPress.org

Ready to get started?Download WordPress

Forums

Saving Custom Field in Attachment Window in WordPress 3.5 (1 post)

  1. Faisal79c2
    Member
    Posted 1 year ago #

    I'm using the code below to add a custom text field in WP 3.5 Attachment Window (from this question # http://wordpress.stackexchange.com/questions/74975/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.

    Any help would be appreciated :)

Topic Closed

This topic has been closed to new replies.

About this Topic