• Resolved Compute

    (@compute)


    As I descriped earlier here:
    http://wordpress.org/support/topic/doesnt-save-custom-field-data-in-media-frame

    The issue was fixed but a new problem appeared:

    If I do not touch the values in my custom fields it does not save the value of the first option of the selectbox.

    The code for my selectbox is here:

    /**
     * Form fields
     */
    function custom_attachment_fields_to_edit( $form_fields, $post ) {
    	$form_fields['size'] = custom_attachment_fields_to_edit_cb( $post );
    
    	return $form_fields;
    }
    add_action( 'attachment_fields_to_edit', 'custom_attachment_fields_to_edit', 10, 2 );
    
    /**
     * Form field callback
     */
    function custom_attachment_fields_to_edit_cb( $post ) {
    	$out_array = array(
    		'one-col' => __( 'One column' ),
    		'two-cols' => __( 'Two columns' ),
    		'full-row' => __( 'Full row')
    	);
    
    	$out = '<select id="attachments-' . $post->ID . '-size" name="attachments[' . $post->ID . '][size]">';
    
    	foreach ( $out_array as $k => $v ) {
    		$selected = selected( $k, get_post_meta( $post->ID, 'attachment_size', true ), false );
    		$out .= '<option value="' . $k . '"' . $selected .'>' . $v . '</option>';
    	}
    
    	$out .= '</select>';
    
    	return array(
    		'label' => __( 'Size' ),
    		'input' => 'html',
    		'html'  => $out,
    	);
    }
    
    /**
     * Save our gallery meta
     */
    function custom_attachment_fields_to_save( $post, $attachment ) {
    	if( isset( $attachment['size'] ) )
    		update_post_meta( $post['ID'], 'attachment_size', $attachment['size'] );
    
    	return $post;
    }
    add_filter( 'attachment_fields_to_save', 'custom_attachment_fields_to_save', 10, 2 );

Viewing 2 replies - 1 through 2 (of 2 total)
  • Since you are already checking isset( $attachment['size'] ), you should probably do an else that updates it to whatever your default value is.

    Or, in the code that checks this metadata, assume whatever your default value is when it is missing.

    It is generally not best practice to save default values to the database when you don’t need to. In this case, we’re not necessarily trying to prevent you from doing that. We just only save these fields when there has been a change.

    I’m looking at options here to make sure we save on insert if there hadn’t been a save yet.

    We have decided not to do anything further for 3.5.

    I would work to improve your default value handling so you can be fully compatible. Metadata will not always exist after all — think about all things uploaded prior to your attachment being updated.

    More in http://core.trac.wordpress.org/ticket/22776. Marking as resolved.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Doesnt save custom field selectbox data in media-frame’ is closed to new replies.