ZIP Files are not allowed in WordPress by default but you might want to have a way to upload them and unpack them. For example, how WordPress uploads and unpacks plugin ZIP files.

The code we will write, you can add to your theme or in an existing plugin. You can also add a form to a shortcode or add it directly to a template.

We will create a separate plugin.

Plugin to Upload ZIP files

In our main class we will store any errors on notices that we could get while processing the upload.

We are also hooking into init to listen for upload action and also to register our shortcode.

The Shortcode

In our method form, we are displaying errors and notices (if there are any) and also the form with our file input and a submit button.

Now you can use zip_upload_form as a shortcode to display the form on a page.

We still do not do anything with the file, so let’s create the method to start the upload process.

Upload Method

Here we are checking the nonce. If such nonce is not submitted, we do not try to process the upload. If it is but it is a wrong one, we skip the next steps.

If all is correct, we are including our uploader class, define the folder where we will upload and start the process. Here our folder is zips.

Once the process if finished, if we have any errors, we will set them in the attribute $errors and display them above the form.

If success, we add such message to the $notices attribute. For the purposes of this tutorial, that is enough. I encourage you to style them to your own liking.

The ZIP Uploader for WordPress

Let’s now define our class for uploading ZIPs. We will use the function unzip_file to unzip the file.

Before we do anything, let’s define the flow we want and then write the code.

  1. Define the main folder to hold our unzipped files,
  2. Define the folder name for files of the current ZIP and create it,
  3. Define a folder to hold the uploaded ZIP and create it,
  4. Upload the ZIP to the folder in step 3.,
  5. Unzip the file and move its contents to the folder in step 2.,
  6. Delete the folder in step 3.

Let’s now create our class includes/class-zip-uploader.php and add this code first:

Here we created some helper methods that will help us when dealing with folder names and errors. We will use get_folder_name to prepare the folder name to be in form of name-of-my-folder without any spaces.

The method get_target_path is used to get the WordPress folder for uploads and with the method get_folder_path we will construct the full path.

Upload

Let’s now define our method for uploading.

Code

In here, you will get this plugin in a zip file which you can upload and use if you want.

This part is available only to the members. If you want to become a member and support my work go to this link and subscribe: Become a Member

Suggestions

Here are some of the suggestions to make it better:

  • style the form, errors and notices,
  • remove the path in notices to the uploaded contents,
  • store the uploaded zips in a custom table or options,
  • display those uploaded zips in a dashboard widget or custom page,
  • map the uploaded zip with the user if logged in (usermeta)

Conclusion

WordPress has a lot of helper methods that help you work with files on your WordPress site.

Have you ever worked with uploading files to custom paths and such? Let us know in the comments below.

Become a Sponsor

Posted by Igor Benic

Web Developer who mainly uses WordPress for projects. Working on various project through Codeable & Toptal. Author of several ebooks at https://leanpub.com/u/igorbenic.

One Comment

  1. Great tutorial! Very useful

    Reply

Leave a reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.