views:

878

answers:

4

Hi

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?

A: 

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.

Chris Ballance
Try setting window.location and make the redirect with javascript.
Chris Ballance
I know that, but output comes from .../wp-admin/menu-header.php:128which is a standard wordpress file.I could try ob_start + ob_* - but this seems to be a little hackish.
dr_bonzo
A: 

I suppose you just have to make sure that wp_redirect() comes before any output has been sent.

SilentGhost
+1  A: 

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.

dr_bonzo
A: 

Load it into template_redirect.

add_action('template_redirect', 'myplugin_template_redirect');

function myplugin_template_redirect() {  
   wp_redirect('http://www.example.com/', 301);
}
Andy