2014-02-27 38 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'); 
相关问题