Leave a Reply

1 comment

  1. it is much easier with WordPress inbuilt function media_handle_upload

    http://codex.wordpress.org/Function_Reference/media_handle_upload

    // These files need to be included as dependencies when on the front end.
    require_once( ABSPATH . 'wp-admin/includes/image.php' );
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
    require_once( ABSPATH . 'wp-admin/includes/media.php' );
    
    // Let WordPress handle the upload.
    // Remember, 'my_image_upload' is the name of our file input in our form above.
    $attachment_id = media_handle_upload( 'my_image_upload', $_POST['post_id'] );
    
    if ( is_wp_error( $attachment_id ) ) {
        // There was an error uploading the image.
    } else {
        // The image was uploaded successfully!
    }
    

    You will need to specify your file control name, then you can call set_post_thumbnail function OR set post meta ‘_thumbnail_id’

    set_post_thumbnail( $post_id, $attachment_id );
    

    EDIT:

    For some reason, on the post edit screen the image file does not even upload.

    Can you double check that you have properly set enctype=”multipart/form-data” attribute to your form tag?