1

I have currently added a custom meta field to upload a media file (image). I can successfully add the upload button to display and it works but I'm having problems saving the field when updating the page.

Code I'm using to create and display the field:

    <?php
      $image = get_template_directory_uri().'/images/image.png';
      echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';

    if ($meta)
    {
     $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0];
    }

     echo '<input name="image-upload-one" type="hidden" class="custom_upload_image value="'.$meta.'" />
     <img src="'.$image.'" class="custom_preview_image" alt="" />
     <br />
     <input class="custom_upload_image_button button" type="button" value="Choose Image" />
     <br/>
     <small>
       <a href="#" class="custom_clear_image_button">Remove Image</a>
     </small>
     <br clear="all" />
     <span class="description">'.$field['desc'].'</span>'; ?>

The above code is from: http://code.tutsplus.com/tutorials/reusable-custom-meta-boxes-part-3-extra-fields--wp-23821 I can't find a way to save the input.

I've created simple text fields/texttext area fields and can save them no problem using the function below but having problems saving image fields:

  <?php if (isset($_POST['text-two']))
    {
       update_post_meta($post_id,'textTwo', esc_attr($_POST['text-two']));
    } ?>

Any help much appreciated.

0

For posting images you should use the wordpress function media_handle_upload

<?php if (isset($_FILES['text-two']))

require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');

$attach_id = media_handle_upload( $_FILES['text-two'], $post_id );

update_post_meta($post_id,'textTwo', $attach_id));

?>

Also check this function wp_handle_uploadhttp://codex.wordpress.org/Function_Reference/wp_handle_upload to handle uploads in wordpress

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.