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.
In our method
form, we are displaying errors and notices (if there are any) and also the form with our
file input and a
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.
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
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.
- Define the main folder to hold our unzipped files,
- Define the folder name for files of the current ZIP and create it,
- Define a folder to hold the uploaded ZIP and create it,
- Upload the ZIP to the folder in step 3.,
- Unzip the file and move its contents to the folder in step 2.,
- 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.
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.
Let’s now define our method for uploading.
In here, you will get this plugin in a zip file which you can upload and use if you want.
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)
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