X

Upload user-submitted files from the frontend

There are many use cases for allowing users to upload files from the frontend, but the trick is making sure the file is uploaded and saved in the WordPress media library correctly. You can easily use standard PHP functions to handle the upload and then store the file wherever you like, but then you won't be able to take advantage of the attachment system that WordPress uses. An image that is uploaded correctly, for example, will have all the various sizes available as well as the meta data, such as title, description and caption. You will also be able to edit all of this from the media library in your WordPress dashboard.

This snippet will handle the upload for you and save the file in your media library correctly. All you need to do is pass the uploaded file array to the function – all uploaded files are stored in the $_FILES array so you just need to loop through that and upload each file from there. Remember that if you want your form to be able to handle file uploads then you need to add the enctype=”multipart/form-data” attribute to the <form> tag.

function upload_user_file( $file = array() ) {

&#x9;require_once( ABSPATH . &#x27;wp-admin/includes/admin.php&#x27; );

      $file_return = wp_handle_upload( $file, array(&#x27;test_form&#x27; =&#x3E; false ) );

      if( isset( $file_return[&#x27;error&#x27;] ) || isset( $file_return[&#x27;upload_error_handler&#x27;] ) ) {
          return false;
      } else {

          $filename = $file_return[&#x27;file&#x27;];

          $attachment = array(
              &#x27;post_mime_type&#x27; =&#x3E; $file_return[&#x27;type&#x27;],
              &#x27;post_title&#x27; =&#x3E; preg_replace( &#x27;/\.[^.]+$/&#x27;, &#x27;&#x27;, basename( $filename ) ),
              &#x27;post_content&#x27; =&#x3E; &#x27;&#x27;,
              &#x27;post_status&#x27; =&#x3E; &#x27;inherit&#x27;,
              &#x27;guid&#x27; =&#x3E; $file_return[&#x27;url&#x27;]
          );

          $attachment_id = wp_insert_attachment( $attachment, $file_return[&#x27;url&#x27;] );

          require_once(ABSPATH . &#x27;wp-admin/includes/image.php&#x27;);
          $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
          wp_update_attachment_metadata( $attachment_id, $attachment_data );

          if( 0 &#x3C; intval( $attachment_id ) ) {
          &#x9;return $attachment_id;
          }
      }

      return false;
}
if( ! empty( $_FILES ) ) {
  foreach( $_FILES as $file ) {
    if( is_array( $file ) ) {
      $attachment_id = upload_user_file( $file );
    }
  }
}

Comments  Leave a Reply

  1. Cool, is there a code that you can preview the file?

    1. I’m sure anything could be done, but it does not have a preview of the file.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!