You have a variable scope problem.
$post_id = wp_insert_post($post_information);
function upload_user_file( $file = array() ) {
require_once( ABSPATH . 'wp-admin/includes/admin.php' );
$file_return = wp_handle_upload( $file, array('test_form' => false ) );
// ...
You set $post_id
outside of your upload_user_file()
function but that means that it is unavailable inside the function where you need it for wp_insert_attachment()
.
If you had debugging enabled as you should when working, you would have spotted that immediately.
Additionally, you are including a couple of core files– for example, require_once( ABSPATH . 'wp-admin/includes/admin.php' );
— which nearly always indicates that you are Doing it Wrong. I have a feeling that you should be using the AJAX API instaed of, what I am guessing you are doing, loading a “handler” file directly.
Related Posts:
- Download an image from a webpage to the default uploads folder
- Is there a hook which fires after all thumbnails are generated?
- How can I add the “Use as featured image” to a custom metabox?
- Frontend Post – Allow Only Image File Upload
- How to stop thumbnail generation from some images and different size thumbnail generate
- How to replace images?
- How to set uploaded image as featured image wordpress
- adding image to post thumbnail from another server
- WordPress Multisite: Images moved to /sites/1?
- How to delete original image file after upload and resize
- An error has occurred during the upload process in wordpress version 5.3.2
- Troubles with code for upload a image
- Each custom image size in custom upload directory?
- How to automatically add rounded corners to thumbnails?
- Uploaded images don’t show in Media Library if there are special characters in IPTC Keywords
- Rename files on upload
- Remove title attribute from images
- How to limit number of images being printer out in “Set Featured Image” pop up?
- How to copy and paste a picture found on the web to wordpress easily
- Get attachment ID of author_meta image – Attachment Metadata
- Black and White thumbnails
- How to Display an Image Uploaded through a Meta Box in a Template?
- HTTP Error when trying to upload image
- Download button for Featured Image in every post – automatically
- Removing Title Tag from Thumbnails
- When Uploading JPEGs, Does WordPress Compress the Original Image?
- Uploading images: ‘ø’ get replaced with ‘ø’
- SSL: How to make customizer images Protocol Relative in WordPress?
- Insert image in comment reply
- Thumbnails are bigger in size than the original image
- add_image_size() and uploading the exact size leaves 1×1 image in uploads
- media_sideload_image with rewritten urls?
- How to set a contact forms total size limit
- SVG Featured image not shown in twitter
- Featured images not displaying at full resolution
- All Images on wordpress site broken
- How to keep WP from recompressing Full Size images
- Alter the image, before save, of a particular image size
- WordPress automatically downscaled images larger that original
- Missing a temporary folder.error when image is uploaded in admin Set temporary Directory – WordPress 5.2.2
- WP fails to upload files to media library, /wp-admin/async-upload.php 403 forbidden
- Add a background image for Custom Post Type archives page
- Images stopped loadding after adding SSl
- Setup Featured image on all posts from thesis thesis_post_image custom field?
- Uploaded images not displaying in network site
- Change image name during uploading sequentially
- How to limit the number of images displayed in the media window?
- How to get the Gallery form/section just like in Gutenberg block?
- Should I not compress my jpeg files before uploading to avoid double compression?
- Is it possible to preserve original images in order to change image size later?
- How do you import images from a URL in your post?
- Get images attached to a specific page
- add_image_size function not working
- What sense makes medium_large_size_h => 0?
- How do I link a featured photo to it’s post?
- Is it possible to enable the ‘Link To’ field under ‘Attachment Display Settings’ for a Featured Image?
- after migration, newly uploaded images won’t show up
- Use default image as var
- Image upload error?
- How to add “alt” attribute for image during upload at WP front-end?
- How to use getimagesize() of image upload?
- How to change image’s author via a function when using GravityForms uploader? [closed]
- How do I display the featured image from an image gallery using a reference in code? (example within)
- Alter the image used for a user who has not uploaded a custom image yet
- Allow users to upload multiple images to their profile
- attach several images to post + gallery
- Serving Images from subdomain in wordpress
- User uploads image as “Featured Image” but WordPress resizes it?
- How to increase media image scale limit?
- WordPress PHP custom function is causing 500 Internal Server Error Connection Timeout
- Can’t upload Images to WordPress – Error 500 Response
- Force WordPress to always generate a thumbnail for original image
- Get post thumbnail in specific size
- WordPress 4.4.2 upload give error, can’t write is upper directory allowed to be written
- How to trigger “wp_handle_upload_prefilter” filter when uploading an image programatically?
- featured image in custom poste type (backend)
- Broken images in media library
- Images are not getting uploaded
- Run command to optimize images after they get uploaded to WordPress
- Where to add my code to auto attach images to existing products
- Declare global variable that can be used on other function
- Set WordPress to Create different images format later, not during upload
- Assigning image to Post-Type won’t work anymore: exceeded time?
- The most of the images don’t appear, although URLs are correct
- Add two images with Sweet Testimonials
- Black and White Thumbnail in WordPress Gallery
- Upload button in metabox not working
- Import existing image as a single post
- Upload unnattached image from frontend
- Getting top Image From the Gallery and printing out a thumbnail with Exact Dimensions
- How do I change a Themes Header Image?
- Media Library is not loading on grid view in admin panel
- Blurry Images WordPress 5.8.1
- Media upload error
- I tried to move the featured image below the paragraphs but now it is displaying twice
- How to import external media items into wordpress media library (without using storage)
- Generate and upload screenshot as featured image for user-submitted post
- Can’t upload svg files in WordPress
- Random images from uploads folder and subfolder
- Is there a way to upscale a media library original image to the nearest whole pixel in WordPress?