WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Saving custom fields for attachments (3 posts)

  1. Vayu Robins
    Member
    Posted 3 years ago #

    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;
    }
  2. Vayu Robins
    Member
    Posted 3 years ago #

    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;
    }
  3. Kaleb.G
    Member
    Posted 3 years ago #

    Thanks for pointing me to that tutorial!

Topic Closed

This topic has been closed to new replies.

About this Topic