0

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.

1

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.)

  • ok but how do I save it when submitting the form on the front end? – Rich Jun 28 '18 at 6:16
  • 1
    Please check the updated answer. – Sally CJ Jun 28 '18 at 6:53
  • Ok, thanks. I tried it but it still didnt save it. – Rich Jun 28 '18 at 7:16
  • 1
    Add var_dump( $atts ); above the foreach loop in the code I provided for saving the data. Then see if the output is what you expected. You may also want to share it (temporarily) on Pastebin.com so that I can inspect the data. Try also to change the attachments to attachments2 in the field's name and in the code I gave. – Sally CJ Jun 28 '18 at 7:26
  • 1
    I've also updated the answer. Hopefully the update helps you. – Sally CJ Jun 28 '18 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.