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)
);
wp_update_post($post);
update_post_meta($current_post,'_thumbnail_id',$attach_id);
set_post_thumbnail( $current_post, $attach_id );