Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

With this sample of code I want to demonstrate that, my meta boxes are working well, entering data into database (postmeta) with the post, except the image files. They are passing the filenames only (i.e.: xyz.jpg). For your understanding I posted a Text Field's code and my File Input Field's code too. I have 5 image fields to let the user upload their product images, for example I enabled only the first image field here.

<?php
function initiate_meta_box() {
    add_meta_box( 'my_fields', 'My Fields', 'my_fields_callback', 'post','normal', 'high' );
}
add_action( 'add_meta_boxes', 'initiate_meta_box' );

function my_fields_callback( $post ) {
    global $post;
    $values = get_post_custom( $post->ID );

    $prodBrand = isset( $values['prodBrand'] ) ? esc_attr( $values['prodBrand'][0] ) : ''; // Text Field

    $prodImage1 = isset( $values['prodImage1'] ) ? esc_attr( $values['prodImage1'][0] ) : ''; // File Field
    $prodImage2 = isset( $values['prodImage2'] ) ? esc_attr( $values['prodImage2'][0] ) : ''; // File Field
    $prodImage3 = isset( $values['prodImage3'] ) ? esc_attr( $values['prodImage3'][0] ) : ''; // File Field
    $prodImage4 = isset( $values['prodImage4'] ) ? esc_attr( $values['prodImage4'][0] ) : ''; // File Field
    $prodImage5 = isset( $values['prodImage5'] ) ? esc_attr( $values['prodImage5'][0] ) : ''; // File Field

    wp_nonce_field( 'my_fields_nonce', 'my_nonce' ); ?>

    <input type="text" name="prodBrand" id="prodBrand" value="<?php echo $prodBrand; ?>" />    
    <input type="file" name="prodImage1" id="prodImage1" value="<?php echo $prodImage1; ?>" />

<?php } // end of function my_fields_callback( $post )

/* SAVING INTO DATABASE */

function save_meta_box( $post_id ) {
    global $post_id;

    $allowed = array (
                'a' => array (
                    'href' => array()
                )
    );

    update_post_meta( $post_id, 'prodImage1', media_handle_upload( wp_kses( $_POST['prodImage1'], $allowed ) ), $post_id );
        exit;
} // end of function save_meta_box( $post_id )

add_action( 'save_post', 'save_meta_box' );
?>

EDIT:

Meanwhile I figured out a minute bug in my code and fixed it.

update_post_meta( $post_id, 'prodImage1', media_handle_upload( wp_kses( $_POST['prodImage1'], $allowed ) ), $post_id );

to

update_post_meta( $post_id, 'prodImage1', media_handle_upload( wp_kses( $_POST['prodImage1'], $allowed ), $post_id ) );

But now, it's taking an Upload Error into db:

O:8:"WP_Error":2:{s:6:"errors";a:1:{s:12:"upload_error";a:1:{i:0;s:212:"File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.";}}s:10:"error_data";a:0:{}}

QUESTION

So, the question is:

  • How can I enable my WordPress Meta Box to upload image into the post as a custom field input?
share|improve this question
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.