WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Doesnt save custom field data in media-frame (4 posts)

  1. Patrick Hesselberg
    Member
    Posted 1 year ago #

    I've tryed to add another form field to the attachments. Seems to Work fine in the post.php edit form, but when I enter the media-frame (when you try to insert media inside a post) it doesnt save the data.

    /**
     * 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 );
  2. Should be fixed in the next nightly, see https://core.trac.wordpress.org/ticket/22577

  3. Patrick Hesselberg
    Member
    Posted 1 year ago #

    That is great - thank you!

  4. Patrick Hesselberg
    Member
    Posted 1 year ago #

    Just updated to the latest nightly (3.5-RC1-22879) and it seems like saves my data if the field is selected first, and then unfocused afterwards.

    Might be the same problem as https://core.trac.wordpress.org/ticket/22593
    but I just wanted to let you know.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.