I have this nice little code coming straight from the Wordpress codex

if($_FILES['user_picture']['size'] != 0):
            if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
            $uploadedfile = $_FILES['user_picture'];
            $upload_overrides = array( 'test_form' => false );
            $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
            if ( $movefile ) {
                $wp_filetype = $movefile['type'];
                $filename = $movefile['file'];
                $wp_upload_dir = wp_upload_dir();
                $attachment = array(
                    'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
                    'post_mime_type' => $wp_filetype,
                    'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
                    'post_content' => '',
                    'post_status' => 'inherit'
                $attach_id = wp_insert_attachment( $attachment, $filename);

The thing is that it works great but it doesn't create all the image size set in the function.php. I don't want to run a plugin that regenerate a thumbnail each time I upload an image, there must be a way to create all those thumbs after upload. How ?


After some investigation on how other script work, I need to get the file back and use the wp_get_image_editor function to make my thumbs... kinda a pain in the know what. Can't believe that wordpress don't resize images after running wp_handle_upload or wp_insert_attachment.

1 Answer 1



$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id,  $attach_data );


$attach_id = wp_insert_attachment( $attachment, $filename);

Fixed it.

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.