I'm using the code below to upload a post thumbnail image through the front end. However, it only seems to upload the original size and not all the various thumbnail sizes e.g. 'medium', 'large' etc. I am getting _wp_attached_file and _thumbnail_id entered into the db no problem, but no other size meta data. The image is added to the correct uploads folder, but only the original size not the additional standard sizes.

Thanks in advance.

$uploaddir = wp_upload_dir();
                  $file = $_FILES['featured' ];
                  $uploadfile = $uploaddir['path'] . '/' . basename( $file['name'] );
                  move_uploaded_file( $file['tmp_name'] , $uploadfile );
                  $filename = basename( $uploadfile );
                  $wp_filetype = wp_check_filetype(basename($filename), null );
                  $attachment = array(
                    'post_mime_type' => $wp_filetype['type'],
                    'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
                    'post_content' => '',
                    'post_status' => 'inherit',
                    'menu_order' => $_i + 1000
                  $attach_id = wp_insert_attachment( $attachment, $uploadfile );
                  $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
                  wp_update_attachment_metadata( $attach_id, $attach_data );

                  $post = array(
                      'ID'             => esc_sql($current_post)

                  set_post_thumbnail( $current_post, $attach_id );

2 Answers 2


wp_generate_attachment_metadata generates metadata for an image attachment. It also creates a thumbnail and other intermediate sizes of the image attachment based on the sizes defined on the Settings_Media_Screen.

wp_generate_attachment_metadata() is located in wp-admin/includes/image.php.

/* just require image.php before wp_generate_attachment_metadata */

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

$attach_data = wp_generate_attachment_metadata( $attach_id, $file );

hope that helps!

  • Thanks for the suggestion Aishan. I had already tried that but it didn't seem to work for me.
    – The Sumo
    Commented Feb 28, 2017 at 14:40
  • @Aishan this file is require for this require_once(ABSPATH . 'wp-admin/includes/image.php'); Thanks for your answer. It works for me. Commented Apr 4, 2018 at 13:12

It seems that the only way to do this is to use media_handle_upload

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.