Save attachment custom fields on front end
I have a front end post form that lets users upload multiple photos and on there I have 2 input fields to try and save custom meta data for each attachment -
$vid_pix = get_post_meta($v_Id, 'vid_pix', false);
if (!empty($vid_pix)) {
foreach ($vid_pix as $vP) {
$Pt = get_post_meta($vP, 'photo_time', true);
$Por = get_post_meta($vP, 'photo_order', true);
echo 'img src="' . wp_get_attachment_thumb_url($vP) . '" alt=""/';
echo 'input type="hidden" name="photo_order" class="photo_order" value="'.$Por.'" /';
echo 'input type="text" name="photo_time" class="photo_time" value="'.$Pt.'"/';
}
}
When the form is submitted I am trying to save it using the following (I commented out failed attempts) -
//$Pt = get_post_meta($v_Id, 'photo_time', false);
//$Por = get_post_meta($v_Id, 'photo_order', false);
// GET ATTACHMENT IDS
if (!empty($vid_pix)) {
foreach ($vid_pix as $vP) {
$post_id = $vP;
$meta = $_POST['attachments'][ $post_id ]['photo_time'];
update_post_meta($post_id, 'photo_time', $meta);
$meta_two = $_POST['attachments'][$post_id]['photo_order'];
update_post_meta($post_id, 'photo_order', $meta_two);
update_post_meta($vP, 'photo_time', $attachment['attachments'][$vP]['photo_time']);
update_post_meta($vP, 'photo_order', $attachment['attachments'][$vP]['photo_order']);
//$Pt = get_post_meta($v_Id, 'photo_time', false);
//$Por = get_post_meta($v_Id, 'photo_order', false);
//$Pt = get_post_meta($vP, 'photo_time', true);
//$Por = get_post_meta($vP, 'photo_order', true);
//update_post_meta($vP, 'photo_time', $Pt);
//update_post_meta($vP, 'photo_order', $Por);
//update_post_meta($v_Id, '_photo_time', $newvidPix);
//add_post_meta($v_Id, 'photo_time', $Pt, false);
However I can not get it to work. Using the following code in functions.php I am able to get everything working on the backend though --
// ADD FIELDS
function mytheme_attachment_fields( $fields, $post ) {
$meta = get_post_meta($post-ID, 'photo_time', true);
$fields['photo_time'] = array(
'label' = 'photo time',
'input' = 'text',
'value' = $meta,
'show_in_edit' = true,
);
$meta_two = get_post_meta($post-ID, 'photo_order', true);
$fields['photo_order'] = array(
'label' = 'photo order',
'input' = 'text',
'value' = $meta_two,
'show_in_edit' = true,
);
return $fields;
}
add_filter( 'attachment_fields_to_edit', 'mytheme_attachment_fields', 10, 2 );
// Update custom field on save
function mytheme_update_attachment_meta($attachment){
global $post;
update_post_meta($post-ID, 'photo_time', $attachment['attachments'][$post-ID]['photo_time']);
update_post_meta($post-ID, 'photo_order', $attachment['attachments'][$post-ID]['photo_order']);
return $attachment;
}
add_filter( 'edit_attachment', 'mytheme_update_attachment_meta', 4);
// Update custom field via ajax
function mytheme_media_xtra_fields() {
$post_id = $_POST['id'];
$meta = $_POST['attachments'][$post_id ]['photo_time'];
update_post_meta($post_id , 'photo_time', $meta);
$meta_two = $_POST['attachments'][$post_id ]['photo_order'];
update_post_meta($post_id , 'photo_order', $meta_two);
clean_post_cache($post_id);
}
add_action('wp_ajax_save-attachment-compat', 'mytheme_media_xtra_fields', 0, 1);
Any ideas on how to get it saving on the front end? Thanks.
Topic attachment-fields-to-edit post-meta attachments forms custom-field Wordpress
Category Web