Upload Multiple Image With media_handle_upload In WordPress

WordPress Front End Multiple File Upload

Upload Multiple Image With media_handle_upload In  WordPress

Upload Multiple Image With media_handle_upload In WordPress

Very easy to uploade multiple image in wordpress media page. Please follow this code-

While customizing WordPress for our suitable cms solutions. We have certain things required from our customers. So it requires front end media upload. Even few things require to gather details and posts from users. If it requires you can also provide your user to upload media files from the front end of your site. May be of my following code can help you to create front end upload with multiple files simultaneously from single from.

HTML form like this-
 <form id="featured_upload" method="post" action="#" enctype="multipart/form-data">
  <input type="file" name="my_image_upload[]" id="my_image_upload[]"  multiple="true" />
  <?php wp_nonce_field( 'my_image_upload', 'my_image_upload_nonce' ); ?>
  <input id="submit_my_image_upload" name="submit_my_image_upload" type="submit" value="Upload" />


The above HTML form handles the user upload files at your sites front-end. Now code for your back end to store the files.

Uploade image code-
// Check that the nonce is valid, and the user can edit this post.
if ( 
  isset( $_POST['my_image_upload_nonce'])
) {
  // The nonce was valid and the user has the capabilities, it is safe to continue.

  // These files need to be included as dependencies when on the front end.
  require_once( ABSPATH . 'wp-admin/includes/image.php' );
  require_once( ABSPATH . 'wp-admin/includes/file.php' );
  require_once( ABSPATH . 'wp-admin/includes/media.php' );
  $files = $_FILES["my_image_upload"];  
    foreach ($files['name'] as $key => $value) {            
            if ($files['name'][$key]) { 
                $file = array( 
                    'name' => $files['name'][$key],
                    'type' => $files['type'][$key], 
                    'tmp_name' => $files['tmp_name'][$key], 
                    'error' => $files['error'][$key],
                    'size' => $files['size'][$key]
                $_FILES = array ("my_image_upload" => $file); 
                foreach ($_FILES as $file => $array) {              
                   // $newupload = my_handle_attachment($file,$post_id); 
          $attach_id = media_handle_upload( $file, -1 );
  if ( is_wp_error( $attachment_id ) ) {
    // There was an error uploading the image.
  } else {
    echo "The image was uploaded successfully!";

} else {

  // The security check failed, maybe show the user an error.

This above code is return image ID(Attachment ID).

It is actually quite simple. Using the media_handle_upload() function reduces a lot of extra work. There is no sense in re-inventing the wheel and usually with WordPress you don’t have to.


uploade image


Leave a Reply

most readable posts

Filter Posts By Category WordPress

If you have lots of posts in your WordPress site, you have probably used the Category and/or Tag filter at the top of the post. These filter are great because they very quickly allow you to limit the kind of posts that are displayed, and let you find the one(s) you are looking for with […]

Read more
Get Custom Post Type With WP_Query In WordPress

In this post I’ll give you an in-depth look into how WP_Query works – let’s get started! This series on WP_Query, you’ll learn how to use WP_Query to query for posts, pages and custom post types. You can query for specific posts and pages or you can run a query to return posts of one […]

Read more
How To Get Post By Taxonomy In WordPress

Taxonomy are basically a way of grouping data in WordPress. The most common default taxonomy used in WordPress are when grouping posts as either ‘categories’ or ‘tags’ and these are named and recorded in the WordPress database as ‘category’ and ‘post_tag’ respectively. Specific categories or tags are called terms. You can see this image for […]

Read more

About Me

We are glad to see you here… If you are visiting for the first time, then, we hope you’ll stay with us in Touch. Codinglead is all about sincere blogging and spreading some basic and some advanced knowledge to you All. As you can see that here that codinglead.com is working on many different categories and concepts (check out in the Menu).

Our Expertise

  • We are providing the resources of the following technologies:
  • Web Development
  • Web Programming
  • Know more.