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 🙂
You can use the javascripts
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
See this post