I am writing a WordPress plugin.
I want to perform a redirect (after creating DB records from POST data, etc…) to other ADMIN page.
Neither header(“Location: …) nor wp_redirect() work – i get
Warning: Cannot modify header information – headers already sent by
which comes from obvious reason.
How do I properly perform a redirect in a WordPress?
On your form action, add ‘noheader=true’ to the action URL. This will prevent the headers for the admin area from being outputted before your redirect. For example:
If you still want to redirect from your plugin admin page to another admin page while using WP add_page* functions then, after processing your request, you can just echo something like this:
This just renders a javascript based redirect to “/whatever_page.php” thus ensuring no trouble with headers already sent by WP as Chris Ballance already said.
Change “/whatever_page.php” to something like “/wp-admin/admin.php?page=whatever_page”
For a link added with add_submenu_page (or related function), use the returned $hook_suffix to add an action to “load-$hook_suffix” and do the redirect there. This is how you hook to the page load before the output has begun.
I think I was doing it the wrong way.
My code was inside a add_menu_page() inside
add_action(‘admin_menu’, …) call
which is probably called later during the request (after page header has been created and displayed).
Moving my code outside of my plugin handles, into main scope worked – it needs some cleanup, and fixes, but redirect works.
Anyway, thanks for the answers.
I suppose you just have to make sure that wp_redirect() comes before any output has been sent.
You need to make sure that nothing is sent to http output before the redirect takes place.
You can set “window.location(‘newlocation’);” and that will still let you redirect after output has been sent to the browser.
Load it into template_redirect.