How to Add Images to Media Library after Uploading them in PHP Programmatically phần 3 (ok)
https://rudrastyh.com/wordpress/how-to-add-images-to-media-library-from-uploaded-files-programmatically.html

How to Add Images to Media Library after Uploading them in PHP Programmatically
When you upload files from WordPress admin area, they will automatically appear in Media > Library. But what if you want to create a custom file uploader specially for your website visitors?
It is easier to organize your uploaded images from WordPress Media Library.
All the photos on the screenshot are taken by me in Roza Khutor resort.
Step 1. As usual, we Begin with HTML Form
What is important to remember here?
I placed the PHP file
process_upload.phpinto the theme directory, if you would like to move it to the other location, do not forget to make changes in the next piece of code on line 4 (I mean code in Step 2).The form must have
enctype="multipart/form-data"attribute. Many forget about it.
You can also create a shortcode for this form, just insert this to the functions.php:
Now you can use just [misha_uploader] in the editor.
Step 2. Process the Uploaded File in PHP and Add the File Metadata to WordPress Database
This is process_upload.php file that I decided to create in my current theme directory.
Optionally we can set the third parameter of the wp_insert_attachment() as a parent post ID, and set the featured image of this post with set_post_thumbnail().
Last updated
Was this helpful?