I am making a file that is used for downloading files that are stored in my website. But when I try to download the file on clicking a button, it does not load the file. I am using header function to do that thing. Can anyone answer me why I cannot use header function while using wp-load.php Or am I doing something wrong. Here is my code:

  require_once(dirname(__FILE__) . '/wp-load.php');
  function lastlink($str){
  $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
  if(preg_match_all("/$regexp/siU", $str, $matches)) {
   $str= end($matches[2]);
  return $str;
 $content_post = get_post($id);
 $content = $content_post->post_content;
 $content = apply_filters('the_content', $content);
 $content = str_replace(']]>', ']]&gt;', $content);
 $name = 'gameofthrones.mp4';
 if(isset($_POST["downloadfile"])) {
 $url=str_replace(" ","%20",$ur);
 header('Expires: 0'); // no cache
 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
 header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
 header('Cache-Control: private', false);
 header('Content-Type: application/force-download');
 header('Content-Disposition: attachment; filename="' . $name . '"');
  header('Content-Transfer-Encoding: binary');
 header('Content-Length: ' . get_remote_size($url)); // provide file size
 header('Connection: close');
<form method="POST">
  <input type="submit" value="DOWNLOAD FILE" name="downloadfile"/>

Have a look and correct me.

closed as off-topic by Mark Kaplun, Mayeenul Islam, bueltge, Tom J Nowell Jul 1 '16 at 14:25

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    You should never create a PHP file in WP that is directly accessed, it's fragile, easy to break, and has security issues – Tom J Nowell Jun 13 '16 at 14:36
  • not sure what you are trying to do, but without even regarding your current issue, you are most likely doing it wrong – Mark Kaplun Jun 13 '16 at 14:36
  • I just want to know how do I use header function to do that, please – Kumar Jun 13 '16 at 14:39
  • Where is this code located? – Tom J Nowell Jun 13 '16 at 22:19

You should never create standalone files to do things like this. This is one of the reasons timthumb turned into a security nightmare, and it's incredibly fragile and dangerous. WordPress is a CMS, and it should handle all requests.

Instead, use GET or POST variables in the URL to do the work inside WordPress, by adding parts to the URL, such as example.com/?download=1

E.g. create your download link like this:

<a href="<?php echo home_url('/?downloadthing=1'); ?>">Download</a>

Then hook into the init event to trigger the download:

add_action( 'init', 'do_the_download' );
function do_the_download() {
    if ( empty( $_GET['downloadthing'] ) {
    // the download code goes here

The same trick can be done for form handlers. For AJAX look at the REST API or the WP AJAX API

I would also keep in mind that what you're trying to do will almost certainly get you in trouble with your host for proxying giant files in PHP for download. It'll be a performance hog which your host will notice. Never mind that you're using it for Game of Thrones videos. Sharing these things via website downloads is the most dangerous and insecure way and is just begging to be caught, even more so if you're the one hosting this script.

  • it did not work – Kumar Jun 13 '16 at 15:57
  • 2
    Can you elaborate on that? The code in my example is not a copy paste solution, and will require modification on your part, as well as an understanding of what filters and hooks are – Tom J Nowell Jun 13 '16 at 22:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.