I need to download a file on click of a link in word press.
I have file url.
I know the download code in php and I added in functions.php.
function downloadFile($f){
header('Content-Disposition: attachment; filename=' . urlencode($f));
header('Content-Type: application/force-download');
header('Content-Type: application/octet-stream');
header('Content-Type: application/download');
header('Content-Description: File Transfer');
header('Content-Length: ' . filesize($f));
echo file_get_contents($f);
}
Now how to call this function on click of a link? What will be the “href” of the link
On the page with the link:
On
download.php
(or whatever name you choose):Edit Or just create a
download.php
page:Instead of creating a function, what you can do is to create a new Page in the WordPress backend and put your php headers in a custom template for that page, like so (simple pdf download):
Then you can simply put the permalink to your new wordpress page as the href on the download link. Permalinks for wordpress pages can be edited at will at the wordpress backend, so you could make it something like:
http://www.example.com/downloadfile
I know it’s old but I recently had this problem as well, hope it helps anyone who ended up here as well!