2013-02-24 66 views
1

我需要使用像我的wordpress插件header('Location: http://location.com');header("Content-disposition: attachment; filename=$fileName"); PHP头,但它似乎不怎么样。我知道他们需要在页面头被调用之前使用,所以我尝试使用init钩子:WordPress的 - 如何使用PHP头()函数

add_action('init', 'test'); 

function test() { 
    header('Location: http://location.com') ; 
} 

但它没有工作。

+1

它怎么没有用?任何错误? – 2013-02-24 11:54:31

回答

1

如果你想重定向任何页面,然后使用wp_redirect()方法..或者,如果你想设置特殊的头,使内容的下载..使用下面sample..code ...

假设你url就像.. http://example.com/download/data.csv

add_action('template_redirect','yoursite_template_redirect'); 
function yoursite_template_redirect() { 
    if ($_SERVER['REQUEST_URI']=='/downloads/data.csv') { 
     header("Content-type: application/x-msdownload",true,200); 
     header("Content-Disposition: attachment; filename=data.csv"); 
     header("Pragma: no-cache"); 
     header("Expires: 0"); 
     echo 'data'; 
     exit(); 
    } 
} 
+0

工作,非常感谢! – 2013-02-24 14:08:24