简体   繁体   中英

Set header redirect after page load

Is it possible to redirect the user to the correct page after headers have been set?

I have developed a plugin in WP that allows users to manage content they have created on the site... for example, they can delete it or disable comments.

The author options appear at the top of the authors post... When the user clicks the button to delete their post, it changes a value in the db to soft delete it:

$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.');
}

The page appears to refresh as the message is shown, but the post is still showing.. the page must be refreshed again before a 'this post does not exist message' appears.

I have tried lots of WP hooks to try and fire wp_redirect before the header but i'm always getting an 'headers already sent' error.

I know I am using WP but I believe this is a http/php limitation so I decided to post it here.

I wanted to do something like:

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

Using output buffering is not an option.

This type of redirecting is available in Laravel and you can even pass a message variable to be shown on the next page so it must be possible some how.

Simple and working solution using javascript. Used this type of reirection in my projects. No problems ever :)

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.');
}

您可以使用javascript

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

There's two ways to do that,

First one using Javascript as @Unni Babu and @ash_8247 answer.

Second way is to use a buffer

//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();
}

See this post

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM