2014-02-27 250 views
0

我正在使用wp_redirect()重定向到一個URL,但它給了我下面的警告,並沒有重定向。我應該什麼時候使用wp_redirect()?是否有人知道wp_redirect()是如何工作的?我需要知道什麼時候應該使用wp_redirect()?

**[Fri Feb 28 03:58:36 2014] [error] [client 127.0.0.1] PHP Warning: 
    Cannot modify header information - headers already sent by (output 
    started at /home/virgo/public_html/others/sites/wepay/wp-content/ 
    themes/twentythirteen/header.php:43) in /home/virgo/public_html/o 
    thers/sites/wepay/wp-includes/pluggable.php on line 875, referer: 
    http://127.0.0.1/others/sites/wepay/?page_id=15** 
+0

在輸出任何內容後,您不能使用它,因爲它使用'Location'標頭髮送重定向,並且標頭出現在HTTP消息的正文之前。 – Paulpro

+2

請參閱http://stackoverflow.com/q/7381661/2126792或http://stackoverflow.com/a/12770075/2126792。 – pschueller

回答

1

標題需要先來,所以你必須把它放在任何輸出之前。這包括錯誤消息。如果你運行一些有問題的代碼會產生通知,這將阻止重定向的運行。

到這裏看看:http://codex.wordpress.org/Plugin_API/Action_Reference

get_header是最新點。

我會建議一旦WP已經安裝,所以你可以使用所有的條件,包括使用$post,但仍然提前。我認爲最合適的鉤子是'wp'。

function wpse_redirect() { 
    if (ENTER A CONDITIONAL) { 
     wp_redirect('redirect-location'); 
     exit; 
    } 
} 
add_action('wp', 'wpse_redirect'); 
相關問題