How to create resized versions of images programatically uploaded with wp_insert_attachment How to create resized versions of images programatically uploaded with wp_insert_attachment wordpress wordpress

How to create resized versions of images programatically uploaded with wp_insert_attachment


You should use another - rather unknown and misunderstood function- media_sideload_image()

 $attach_id = media_sideload_image($filename, $post_id);

This function will handle the "upload" process and will follow the normal workflow of uploading images to wordpress which includes thumbs generation as defined in your custom image sizes


Kaput, your code is perfectly fine, but seems your initial image is not in the uploads folder. See what the documentation says:

Location of the file on the server. Use absolute path and not the URI of the file. The file MUST be on the uploads directory. See wp_upload_dir().

Just make sure you upload the image in the uploads directory first.