I'm using the answer for this previous question (link below) to generate a custom meta box with media upload functionality:

Add "upload media" button in meta box field

I've managed to get it working but I'm having problems with saving the field.

Can anyone help?

Any help much appreciated.

This is the code I'm using to generate the meta box:

Fields inside meta box:

    <input id="upload_image" type="text" size="36" name="upload_image" value="" />
    <input id="upload_image_button" type="button" value="Upload Image" />

Javascript code:

    jQuery(document).ready( function( $ ) {

      $('#upload_image_button').click(function() {

       formfield = $('#upload_image').attr('name');
       tb_show( '', 'media-upload.php?type=image&amp;TB_iframe=true' );
       return false;

      window.send_to_editor = function(html) {
      imgurl = $('img',html).attr('src');


I can create simple text fields, checkboxes etc but struggling with the media upload box.

When creating a text field, I use:

    $titleOne = get_post_meta(get_the_id(), 'titleOne', true); 

    <input type="text" name="title-one" class="title-one" placeholder="Title" value="<?php echo esc_attr ( $titleOne );?>"/>

When saving with a text box, I use the below code in a save function:

    if (isset($_POST['title-one'])) {
        update_post_meta($post_id,'titleOne', esc_attr($_POST['title-one']));

All works with no problems.

  • 1
    Can you explain a little further/post the code that doesn't work? It's also worth mentioning that answer is a little out of date. WordPress 3.5 completely overhauled the media library & dumped thickbox. Check out this great tutorial on using the new media uploader API. Feb 10, 2015 at 12:10
  • TheDeadMedic, please see edited code above. Hope this helps
    – rikardo85
    Feb 10, 2015 at 13:05

1 Answer 1


use these:

$imgurl = isset( $values['upload_image'] ) ? esc_attr( $values['upload_image'][0] ) : '';

<input id="upload_image" type="text" size="36" name="upload_image" value="<?php echo $imgurl; ?>" />

<input id="upload_image_button" type="button" value="Upload Image" />

if( isset( $_POST['upload_image'] ) )
    update_post_meta( $post_id, 'upload_image', wp_kses_data( $_POST['upload_image'] ) );
  • 1
    Please explain why this helps.
    – kaiser
    Jun 2, 2016 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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