Is it possible to redirect a user in WP without the header/wp_redirect being before header()?
I have a plugin that allows users to manage content they have created on the site... for example, they can delete it or disable comments
It's quite basic so i'm just using a GET request.. ie.
?action=comments&status=disable
When this button is click the action is performed but nothing happens on the page and the url is now: www.domain.com/user_data?action=comments&status=disable
what I want to do is on success redirect the user back to www.domain.com/user_data
I have tried wp_redirect() and header() but they give 'headers already sent warnings'
I'm not sure how i can put that before the header when this code is within my plugin.
$query = $this->db->update(
//do something
);
if($query == 0)
{
$this->setMessage('error', 'There was an error: ' . $this->db->print_error());
}
else
{
wp_redirect(site_url('/user_data/'));
}
template_redirect
hook could be used for redirects before headers are sent. – birgire Jul 4 '15 at 4:16function my_redirect($page) { wp_redirect($page);exit; }
add_action('template_redirect', 'my_redirect')
it seems it is specifically designed to load different templates. Any ideas? – Goodbytes Jul 4 '15 at 7:05wp_redirect
which when unset is ignored, then hooked this onto init or template_redirect, then when I want to redirect I set the flag which should cause it work but it's saying headers have been sent. – Goodbytes Jul 4 '15 at 8:06