2013-02-24 73 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