How to serve file downloads from WordPress plugin

Every once in a while plugins you develop will have to serve dynamically generated files. Eg. you have some data and you want the user to be able to download it in CSV format. Generally speaking that is not such a hard task, you figure out what headers you need to send and after that you serve the file content. However, due to the wordpress actions/hooks system you’ll have to hook somewhere before other headers are sent.

  1. Where to hook your code
  2. Design your actual solution

Where to hook your code

The most appropriate place to hook your code is the init hook. Most of WordPress is loaded at that stage and if you run your function with priority 1 and place an exit at the end of it you should achieve returning the file and stopping any other execution for that particular request.

Design your actual solution

The solution is not very complicated so I’ll just paste it below:

What this does is return a CSV based on the type $_GET[‘parameter’], obviously I’m missing the most of the business logic, but the principle is there.

You can download the code for this how-to in
About Pavel Petrov 2 Articles |  17 How-tos
Pavel is a senior developer for the last 7 years, with extended interest in Linux administration, WordPress and Symfony.

Be the first to comment

Leave a Reply

Your email address will not be published.


*