• Hi.

    I recently did this tutorial on Nettuts: http://net.tutsplus.com/tutorials/wordpress/creating-custom-fields-for-attachments-in-wordpress/

    The tutorial was just missing one important factor, or maybe I missed it, but I cant seem to figure out how to save the custom field data for attachments.

    How would I run this function to save the data?

    function my_image_attachment_fields_to_save($post, $attachment) {
    	// $attachment part of the form $_POST ($_POST[attachments][postID])
    	// $post attachments wp post array - will be saved after returned
    	//     $post['post_type'] == 'attachment'
    	if( isset($attachment['my_field']) ){
    		// update_post_meta(postID, meta_key, meta_value);
    		update_post_meta($post['ID'], '_my_field', $attachment['my_field']);
    	}
    	return $post;
    }

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Vayu Robins

    (@vayu)

    This was finally resolved at the Nettuts tutorial.

    add_filter("attachment_fields_to_edit", "my_image_attachment_fields_to_edit", null, 2);
    add_filter("attachment_fields_to_save", "my_image_attachment_fields_to_save", null, 2);
    
    function my_image_attachment_fields_to_edit($form_fields, $post) {
    $form_fields["custom6"]["label"] = __("Custom Field with Helpful Text");
    $form_fields["custom6"]["value"] = get_post_meta($post->ID, "_custom6", true);
    $form_fields["custom6"]["helps"] = "Put helpful text here.";
    return $form_fields;
    }
    
    function my_image_attachment_fields_to_save($post, $attachment) {
    if( isset($attachment['custom6']) ) {
    update_post_meta($post['ID'], '_custom6', $attachment['custom6']);
    }
    return $post;
    }

    Thanks for pointing me to that tutorial!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Saving custom fields for attachments’ is closed to new replies.