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
- How do I display a PDF thumbnail as a link to the PDF without uploading the image
- Featured Image add tab
- Filter to remove image dimension attributes?
- Stop wordpress from hardcoding img width and height attributes
- How to disable generation of default image sizes for some custom post types?
- How to delete resized (cropped) image uploads and prevent future resizing?
- WordPress adding scaled images that don’t exist (1536×1536 and 2048×2048)
- After Moving a Site to Another Domain, All Images Are Lost
- Featured image shortcode
- Using Images in WordPress – Best Practices
- How to find attachment by it’s name?
- Cropped featured image replaces original image in gallery
- Image Scaling using get_the_post_thumbnail issue in WordPress
- Prevent image upload unless exact size
- How to create thumbnails with a fixed width, so all of them will have the same width?
- Use Media upload in custom widget on wordpress 3.5
- Image resize with image url
- How can you upload an image from within a settings page?
- How to increase media image scale limit?
- How to upload image without post ID using the new media uploader?
- How to output placeholder image if no featured image set?
- Featured Images on Front Page
- Uploading an image as featured image from frontend form
- Show prev and next post links for parent post of current image in attachment page?
- How to upload images from my directory into my wordpress?
- could not upload image in media library
- Add a featured image in my theme?
- Why images/photos after being uploaded to my wordpress site get slightly blurry?
- Featured Image .svg height and width 1px only
- Images in upload folder with hexadecimal names
- How to check if user is uploading/setting an image as a featured image?
- Images are randomly deleted from server
- Restrict Image Uploads to a Certain File Type
- Rename media files generated during upload
- add_image_size() to crop images into squares?
- Lazyload post thumbnails
- wordpress media library shows empty images
- Exclude featured image from gallery in wp-admin
- Upload .doc to blog
- media sideload image and Google Content
- Faster way to put images into a blog post by using Add Media dialog
- Handling image uploads without thickbox
- Disable all resizing and compression
- Loose images in uploads folder
- Update old Post image metadata to ‘thumbnail’ wp_attached_file
- Force minimum image size and aspect ratio when uploading images
- having issue while uploading An error occured in the upload. Please try again later
- Convert uploaded GIF to non animated image automatically
- Ho to add “Create Slider” option to default “Add Media” popup?
- WP keeps looking up post thumbnail on https even though wp-config says http
- Images in Media aren’t showing, not broken
- Fixing image names for a site being restored
- Upload Image using wp_insert_attachment and file url don’t have file extension
- using media_sideload_image with a file from theme directory
- Images uploaded to the media library has strange colors. However, in the edit screen the color is ok
- Blank upload.php page
- 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?
- 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?
- Generating a certain sized thumbnail on the fly?
- Get images attached to a specific page
- add_image_size function not working
- Is it possible to enable the ‘Link To’ field under ‘Attachment Display Settings’ for a Featured Image?
- Use default image as var
- Image upload error?
- How to add “alt” attribute for image during upload at WP front-end?
- 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)
- Using different size thumbnails on different parts of my site… with functions.php?
- attach several images to post + gallery
- Serving Images from subdomain in wordpress
- WordPress PHP custom function is causing 500 Internal Server Error Connection Timeout
- Can’t upload Images to WordPress – Error 500 Response
- 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?
- Broken images in media library
- Images are not getting uploaded
- Declare global variable that can be used on other function
- Assigning image to Post-Type won’t work anymore: exceeded time?
- Black and White Thumbnail in WordPress Gallery
- Upload button in metabox not working
- Upload unnattached image from frontend
- Getting top Image From the Gallery and printing out a thumbnail with Exact Dimensions
- How to get WordPress to resize images for srcset?
- Edit multiple posts featured image