WordPress.org

Support

Support » Alpha/Beta » [Resolved] Doesnt save custom field data in media-frame

[Resolved] Doesnt save custom field data in media-frame

  • Compute

    @compute

    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 );
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] Doesnt save custom field data in media-frame’ is closed to new replies.
Skip to toolbar