Creating a button or a link to download a file from WordPress in text is pretty simple: add an image and point it to an URL of the file. In this tutorial you will learn how to programmatically download a file.
To follow this tutorial you should have some basic knowledge about WordPress, WordPress themes and plugins. I will show you snippets of code that you could use inside a plugin or even a specific theme. We will not create a plugin here, but you can start your own code with the PHP comments to start a WordPress plugin.
Before we get to the code part we must first understand how a file will be downloaded. We will need to create a button or a link with the URL that will contain some information about the file we want to download. This will be a link to the file with a Query string that will have the ID of our file.
File with an ID is an attachment in WordPress that was uploaded using the Media Uploader in WordPress or uploaded programmatically using the WordPress API.
We will then check if this Query String is set and if it contains an ID. If that statement is true, we will get the file and send headers and the content from the server using the information from the file.
This could be used in different situations. Here are some of them:
- a site with photos where users or visitors can download the photo
- a portfolio site where you want your visitors to download your portfolio book or something similar
- a knowledge base with various PDF file or files in other formats
- a wallpaper site
The Download button
Now that we understand how to build a button to download a file from WordPress we can start to code. We will create a function that will output a link with the URL to the file alongside with the Query Strings. This function can be used and called to your own liking.
By passing an ID of the file to this function we will create a link that will get the permalink from the ID and also the title of the file. At this moment we would only send the user of this button to the attachment page of this file. By using the next two functions we will change that behavior into a download rather than a visit to a page.
Checking the Query Strings
In the previous function we have set two Query Strings with the permalink to the file and they are the indicator that a download should start. We will check them before the rendering of the page starts. This is done by using the WordPress action init:
This function is added to the action array of the name init and it will be called before the rendering starts. By doing it like that we can send a different header from the server and stop the rendering of the page.
In this example we are not checking the value of the keys in the query strings but only checking if they are set. You can strengthen your function by checking the values and also checking the wpnonce that would be set also in the previous function where we have created the link.
If everything is set then this function will call another function that will create the actual download.
Downloading the file
This is the final function that will create the download. Here we are getting the file from the ID using the WordPress API and if there is a file with the ID that was provided, then we will create the download.
By using some WordPress API and simple PHP functions we can create a great plugin or a theme functionality to download a file from WordPress. This examples can be extended as you wish and can be used for any application you are building upon WordPress.
Have you ever programmatically downloaded a file from WordPress? Do you have other techniques for this? Please share your tips in the comments below or just post what is on your mind 😉