页面加载后设置标题重定向

时间:2015-07-04 14:34:58

标签: php wordpress redirect

是否可以在设置标题后将用户重定向到正确的页面?

我在WP中开发了一个插件,允许用户管理他们在网站上创建的内容...例如,他们可以删除它或禁用评论。

作者选项显示在作者帖子的顶部...当用户单击按钮删除他们的帖子时,它会将数据库中的值更改为软删除它:

$result = $this->db->update(
    $this->_table,
    array('active' => $status), // 0 / 1
    array('id' => $id)
);

if($result > 0)
{
    $this->setMessage('success', 'Post deleted.');
}
else
{
    $this->setMessage('error', 'Some error.');
}

页面似乎在显示消息时刷新,但帖子仍然显示..页面必须在“此帖不存在消息”出现之前再次刷新。

我已尝试过很多WP挂钩尝试在标头之前触发wp_redirect,但我总是收到“已发送标头”错误。

我知道我正在使用WP,但我相信这是一个http / php限制所以我决定在这里发布。

我想做类似的事情:

if($result > 0)
{
    header('Location: ...');
}
else
{
    $this->setMessage('error', 'Some error.');
}

使用输出缓冲不是一种选择。

这种类型的重定向在Laravel中可用,您甚至可以传递一个消息变量以显示在下一页上,因此必须有一些如何。

3 个答案:

答案 0 :(得分:1)

使用javascript的简单工作解决方案。在我的项目中使用这种类型的重定向。没有问题:)左

if($result > 0)
{
    echo "<script>parent.self.location='ENTER YOUR URL HERE';</script>";//enter location..example www.google.com, or profile.php etc
}
else
{
    $this->setMessage('error', 'Some error.');
}


答案 1 :(得分:0)

您可以使用javascripts

window.location.href='enter_your_url_here.php';

答案 2 :(得分:0)

有两种方法可以做到这一点,

第一个使用Javascript作为@Unni Babu和@ ash_8247回答。

第二种方法是使用缓冲区

//allow redirection, even if my theme starts to send output to the browser
add_action('init', 'do_output_buffer');
function do_output_buffer() {
    ob_start();
}

请参阅此post

相关问题