I would like to make XML files downloadable, instead of allowing the browser to displaying them inline.
I know I can use the Content-Disposition: attachment
HTTP header (of course, better solutions are welcome!). I want to add this header in all HTTP responses when a user tries to download an XML file.
I can use the following PHP code:
header('Content-Disposition: attachment; filename="the_filename.xml"');
The problem is: when I can call this instruction? What hook should I use and how?
Actually, my recommendation would be to do things a bit differently. You can add a custom rewrite endpoint to WordPress to handle these files specifically.
For example, the URL
http://site.com/download-xml/the_filename
would automatically download the specified file as an attachment.First, you need to add a custom rewrite endpoint to set this up:
This conveniently also adds a query variable so we can check to see if this endpoint is being used during a standard template redirect.
In the function above, you can do whatever you need to download the file. Load it from the
/wp-content/uploads
directory as a stream, set the file headers (with content-disposition set to “attachment”), dynamically generate content based on something in the database, whatever you want.Just be sure to have the
exit()
call at the end, otherwise WordPress will try to execute its regular template redirection calls and you’ll get some ugly “headers already sent” errors later down the road.You will need to take a look at Plugin API/Action Reference/send headers
Example:
I think you can use the
parse_request
action hookparse_request