It appears you’ve taken the super long winded route and done each step of the upload manually.
But why not use the sideloading function that does it all for you? The majority of your code can be replaced with this:
// 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' );
$attach_id = media_handle_upload( 'featured_image', $new_job_id );
if ( is_wp_error( $attach_id ) ) {
// There was an error uploading the image.
} else {
// The image was uploaded successfully!
}
From there you can set your post thumbnail etc, look up the documentation for media_handle_upload
for more details, and remember to add a nonce check to your uploader or your site will probably get hacked
Related Posts:
- Minimum Dimensions Requirement for Featured Image?
- media-library: how to insert thumbnails with a fixed width, regardless of landscape/portrait orientation
- How to modify Media Library images DPI [ image quality down to make small in size ] [closed]
- How to get image title/alt attribute?
- How to Fix HTTP Error When Uploading Images?
- How to disable WordPress from creating thumbnails?
- How do you get the post thumbnail size?
- set_post_thumbnail_size vs add_image_size
- Generate Thumbnails only for Featured Images
- How to get all featured image sizes and their URLs?
- How to Dynamically Resize WordPress Image On-The-Fly (custom field/theme option)
- Display thumbnail only if requested size exists
- the_post_thumbnail responsive srcset not populating with custom image size
- How to add multiple image sizes of the same size with add_image_size
- New image size version only for the Featured Image file
- Can’t extract and set SVG dimensions
- Set Featured Image Front Frontend Form
- Getting custom-sized featured image’s URL?
- Showing a placeholder/default img, if no featured image is set
- Featured image with the same height thumbs returns wrong thumb
- How to change the naming of the resized image files
- Add Image Dimensions to Media Library Tab on Media Upoader
- Change Output for Images in Content
- Can WordPress crop only the post thumbnails?
- Allow user to manually set crop area of default/custom image sizes on upload [closed]
- How to get the image sizes, in a function, as per the sizes mentioned in the media settings?
- Resizing Images to lower Disk Space Using Plugins
- How to change _wp_attachment_metadata specific value in SQL?
- Autogenerated Thumbnail compression depending on size
- Only create image size when used in featured image
- How can I get a different image size, if all I have is the link?
- How to get custom image sizes into media uploader dropdown?
- WordPress resizes image to the same size as uploaded
- add_image_size and add_filter(‘image_size_names_choose’, ‘my_custom_image_sizes’) not working with wordpress 3.5.2
- wordpress responsive image srcset not working
- Customising the size of images served in Visual Composer elements [closed]
- Custom post type thumbnail / Media Library WP_DEBUG notice [closed]
- How do I get a random image from subset of images in media gallery?
- How to scale up post thumbnails/featured image?
- Why are extra 92 pixel square thumbnails created?
- wp-cli regenerate media is not working for custom sizes
- Upload 3 different featured image sizes to WordPress
- Remove dimensions from filename featured image
- Use wp_remote_get to add image to media library
- Can a thumbnail of an animated gif be animated?
- Replicate featured image functionality
- WordPress Uploader Editor shows broken image
- Images are randomly deleted from server
- add_image_size not working with the_post_thumbnail
- Need clear explanation about medias/files/attchments
- Frontend Post – Allow Only Image File Upload
- Why does the Hard Crop option Scale?
- How do I set the default “feature image” size?
- How to get full absolute url for post attachment?
- Thumbnail Cropping with add_image_size?
- thumbnails are getting clipped down, want to resize instead
- How check that there is post thumbnail feature image or not on wordpress?
- If I Regenerate Thumbnails, would WordPress delete the existing Thumbnails and replace them with ‘new copies’?
- How to get cropped thumbnail source for custom post type
- set a chosen image-file-input as a featured image from wp frontend
- Exclude featured image from attachment loop
- offload media to external services [closed]
- 2 image size for post
- adaptive featured images wordpress 4.4 grid
- Featured Image Not Working when loaded from sub-domain
- How can I see where in my themes and plugins certain image sizes are used?
- Generate image for newly added size
- How to both crop (with tool) and resize thumbnails having only a max width constrain?
- “Medium” size image displayed instead of “thumbnail” in only one post, other posts are fine
- wp_get_attachment_image_src match smallest edge
- How to replace featured image upload option with the post media image option
- How to create resized versions of images added with wp_insert_attachment
- Original image not saving?
- WordPress returns old thumbnail size after changing media settings
- Custom image size in the_content loop
- remove and disable image link in functions.php
- specify size of post preview image (not post thumbnail)
- Display featured image file size, extension, filename, type, orientation?
- Redirect to another page using contact form 7? [closed]
- the_post_thumbnail(‘my_image_size’) always outputs full image
- How to bring files from the upload directory to the media gallery?
- Server B handling add_image_size() differently than Server A
- Post thumbnail throwing size limit parameters in
- Get thumbnail image according to the screen
- Image with large file size not cropping
- What can I do if I set a gif as featured image? no thumbnail when i share on facebook
- images not showing despite using add_theme_support(‘post-thumbnails’);
- Resizing thumbnails retrospectively
- Trying to show link “Use as featured image” from Attachment window from t31os script
- post thumbnail: increase dimension if smaller than… Possible?
- WordPress image crop probems
- Generating very small sized thumbnails from featured image
- Make image full width
- a tricky question about featured image
- How risize a thumbnail?
- Unrestricted height thumbnails
- custom thumbnail image size not working
- WordPress not creating “large” thumbnails
- Is wordpress compressing my images? if so, how to prevent it?
- Custom caption output for custom image size