How to add an image to the "Media library" of WordPress

Tutorial teaching how to add an image to the "Media library" of WordPress by PHP.

You will create an HTML form to send the image and a PHP file that will process the file and "insert it" into WordPress.

How to add an image to the "Media library" of WordPress

When you upload files from the WordPress admin area, they will automatically appear in Media> Library. But what if you want to create a custom file loader especially for the visitors to your site?

Step 1. As usual, we start with an HTML Form

<form action = "<? php echo get_stylesheet_directory_uri ()?> / process_upload.php" method = "post" enctype = "multipart / form-data"> Your photo: <input type = "file" name = "profilepicture" size = "25" /> <input type = "submit" name = "submit" value = "Submit" /> </ form>

Important notes

I placed the PHP file process_upload.php in the theme directory, if you want to move it to the other location, be sure to make changes to the next part of the code in the 4 line.

2). The form must have enctype = "multipart / form-data" attribute. Many forget that.

You can also create a short code for this form, just insert this in the file functions.php of what is being used.

add_shortcode ('misha_uploader', 'misha_uploader_callback'); 'mysql_message ()', 'mysql_name ()', 'mysql_name', 'mysql_name', 'mysql_name', 'mysql_name' = "profilepicture" size = "25" /> <input type = "submit" name = "submit" value = "Submit" /> </ form> '; }

Step 2. Process the uploaded file in PHP and add file metadata to the WordPress database

<? php // WordPress environment require (dirname (__ FILE__). '/../../../wp-load.php'); $ wordpress_upload_dir = wp_upload_dir (); // $ wordpress_upload_dir ['path'] is the full server path to wp-content / uploads / 2017 / 05, for multisite works good as well // $ wordpress_upload_dir ['url'] the absolute URL to the same folder, actually we do not need it, just to show the link to file $ i = 1; // number of tries when the file with the same name already exists $ profilepicture = $ _FILES ['profilepicture']; $ new_file_path = $ wordpress_upload_dir ['path']. '/'. $ profilepicture ['name']; $ new_file_mime = mime_content_type ($ profilepicture ['tmp_name']); if (empty ($ profilepicture)) die ('File is not selected.'); if ($ profilepicture ['error']) die ($ profilepicture ['error']); if ($ profilepicture ['size']> wp_max_upload_size ()) die ('It is too large than expected.'); if (! in_array ($ new_file_mime, get_allowed_mime_types ())) die ('WordPress does not allow this type of uploads.'); while (file_exists ($ new_file_path)) {$ i ++; $ new_file_path = $ wordpress_upload_dir ['path']. '/'. $ i. '_'. $ profilepicture ['name']; } // looks like everything is OK if (move_uploaded_file ($ profilepicture ['tmp_name'], $ new_file_path)) {$ upload_id = wp_insert_attachment (array ('guid' => $ new_file_path, 'post_mime_type' => $ new_file_mime, 'post_title '=>' ',' post_status' => 'inherit'), $ new_file_path ); // wp_generate_attachment_metadata () will not work if you do not include this file require_once (ABSPATH. 'wp-admin / includes / image.php'); // Generate and save the attachment targets into the database wp_update_attachment_metadata ($ upload_id, wp_generate_attachment_metadata ($ upload_id, $ new_file_path)); // Show the uploaded file in browser wp_redirect ($ wordpress_upload_dir ['url']. '/'. Basename ($ new_file_path)); }

Conclusion of How to Add an Image to the Library

Optionally, we can define the third parameter of wp_insert_attachment () as a primary posting ID and define the image of this post with set_post_thumbnail ().

Avoid making changes or new implementations in the production environment.

Make a copy of your WordPress Website in the local environment and run all the necessary tests.

Do not forget to perform a complete backup of the files and database.

Rate This Article!

Click on the stars to give a note of 1 to 5.

1 star2 stars3 stars4 stars5 stars (No Ratings Yet)
Loading ...

Doubts? Make a comment below or send us a message by clicking here.

Your email address will not be published. Required fields are marked with *