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


On the back-end, you're retrieving the data like this:

$meta = $_POST['attachments'][ $post_id ]['photo_time'];
$meta_two = $_POST['attachments'][$post_id]['photo_order'];

So on the front-end, you should set the field's name in this format: attachments[<?= $vP ?>][KEY] where KEY could be either photo_order or photo_time.

echo '<input type="hidden" name="attachments[<?= $vP ?>][photo_order]" class="photo_order" value="'.$Por.'" />';
echo '<input type="text" name="attachments[<?= $vP ?>][photo_time]" class="photo_time" value="'.$Pt.'"/>';

UPDATE

Saving the data:

Check the code in the UPDATE #2 section below.

Use that code in place of what you have (tried) — the code after this in your question:

When the form is submitted I am trying to save it using the following (I commented out failed attempts) -

UPDATE #2

It's probably preferred to wrap the code (for saving the data) in a function, like this:

add_action( 'init', 'my_save_vid_pix_data' );
function my_save_vid_pix_data() {
    if ( ! isset( $_POST['attachments'], $_POST['v_Id'] ) ) {
        return;
    }

    $atts = (array) $_POST['attachments'];
    $post_id = absint( $_POST['v_Id'] );
    $vid_pix = get_post_meta($post_id, 'vid_pix', false);

    foreach ((array) $vid_pix as $vP) {
        if ( ! isset( $atts[ $vP ] ) || ! is_array( $atts[ $vP ] ) ) {
            continue;
        }

        update_post_meta($vP, 'photo_time', $atts[ $vP ]['photo_time']);
        update_post_meta($vP, 'photo_order', $atts[ $vP ]['photo_order']);
    }
}

(Add that function to the theme's functions.php file.)

And add a hidden input field (named v_Id) after this foreach loop:

if (!empty($vid_pix)) {
foreach ($vid_pix as $vP) {
...
echo '<input type="text" name="attachments[<?= $vP ?>][photo_time]" class="photo_time" value="'.$Pt.'"/>'; 
}

echo '<input type="hidden" name="v_Id" value="' . $v_Id . '">';
 }

(Please refer to your question for the $v_Id variable.)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.