Add Images to WordPress Media Library after uploading in PHP

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?

Under this image you will find a PHP script that can upload files to your uploads directory and add them automatically to WordPress Media Library.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

Your Photo:

What is important to remember here?

  • I placed the PHP file process_upload.php into 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:

add_shortcode( 'misha_uploader', 'misha_uploader_callback' );

function misha_uploader_callback(){
	return '
Your Photo:
'; }

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.

 wp_max_upload_size() )
	die( 'It is too large than expected.' );
if( !in_array( $new_file_mime, get_allowed_mime_types() ) )
	die( 'WordPress doesn\'t allow this type of uploads.' );
while( file_exists( $new_file_path ) ) {
	$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'     => preg_replace( '/\.[^.]+$/', '', $profilepicture['name'] ),
		'post_content'   => '',
		'post_status'    => 'inherit'
	), $new_file_path );

	// wp_generate_attachment_metadata() won't work if you do not include this file
	require_once( ABSPATH . 'wp-admin/includes/image.php' );

	// Generate and save the attachment metas 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 ) );


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().

Misha Rudrastyh

Misha Rudrastyh

I develop websites since 2008, so it is total of 13 years of experience, oh my gosh. Most of all I love love love to create websites with WordPress and Gutenberg, some ideas and thoughts I share throughout my blog.

Need some developer help? Contact me