Today while writing a WordPress plugin, I found out that using just wp_redirect() function alone doesn’t work really.
After some investigation I managed to figure it out.
So what’s the problem in the first place? Well, let me give you an example.
In my plugin I wanted to redirect the user to a different page of my plugin if the form was successfully submitted, that is, with all the required fields filled.
Of course, after all the form input checking, I simply added this line:
wp_redirect(get_option('siteurl').'/wp-admin/options-general.php?page=my_plugin/my_plugin.php');
The result was an error:
Warning: Cannot modify header information - headers already sent by (output started at D:\quick_job\wordpress\wp-admin\admin-header.php:18) in <strong>D:\quick_job\wordpress\wp-includes\pluggable.php</strong> on line <strong>868</strong>
The reason for that is that the admin header (HTML) of WP admin area is already being outputted before the wp_redirect() function does its magic. And as you know, there must be no output whatsoever before the php function header() is executed.
So to solve this, you need to do this.
In the form action tag url, you need to add “noheader=true”. You can also add this to links and so on..
The “noheader=true” will make WP wait before the it outputs any of the HTML.
But if the form was submitted and there are some errors in input fields, the page will show up empty. For that, you need to add a simply check:
if (isset($_GET['noheader'])) require_once(ABSPATH . 'wp-admin/admin-header.php');
The above code will check if there is “noheader=true” set in the URL and then output the HTML code of the admin-header.php file which is the one that makes this whole problem.
Here’s a quick full code of this solution.
<?php
if (isset($_POST))
{
$error_found = FALSE;
// Some input field checking
if ($error_found == FALSE)
{
// Use the wp redirect function
wp_redirect(get_option('siteurl').'/wp-admin/options-general.php?page=my_plugin_page2/my_plugin_page2.php');
}
else
{
// Some errors were found, so let's output the header since we are staying on this page
if (isset($_GET['noheader']))
require_once(ABSPATH . 'wp-admin/admin-header.php');
}
}
?>
<div class="wrap">
<h2>wp_redirect test</h2>
<form method="post" action="<?php echo get_option('siteurl'); ?>/wp-admin/options-general.php?page=my_plugin/my_plugin.php&noheader=true">
<p class="submit">
<input type="submit" class="button-primary" value="Submit form" />
</p>
</form>
</div>
Simple, right? Enjoy ;)

This was very helpful for my new plugin. Thanks!
Pingback: Wordpress wp_redirect, no header and CSS style - LessThanWeb
Excellent post!
I came across this issue on WP 3.0+. On previous WP releases my plugin works correctly, but WP update brings this “headers already sent” errors.
And your post save me hours of debuging.
Thanks a lot – this helped me out.
Hello, I am implementing an plugin which will display the records from a database. it has a delete option. And i wanted to redirect to the main page of this plugin page ( List of records page) after the delete action. But unfortunately, after delete action its not redirecting to that page. can you please help me on this?
I am posting my code below.
code:
<a href="options-general.php?page=my-first&id=&delete_status=1″ > Delete
Hi Dev,
Can you post a big more of the PHP code?
That HTML code you posted is missing “&noheader=true”
Pingback: voragine.net » Blog Archive » Cómo resolver los errores que prodice wp_redirect y hacer una redirección de página en WordPress
This helped me out – thank! Where did you get this information from? Is it in the docs somewhere?
Thanks for this, I had spent hours deleting spaces and rechecking files. Finally worked out it seemed to occur on pages with redirects.
// OLD LINE – was being processed before pageload complete
wp_redirect(get_permalink($firstchild->ID));
//NEW LINE: using javascript AFTER page has fully rendered :-)
echo ‘location.href=”‘.get_permalink($firstchild->ID).’”‘;
Hi, Can you explain further how you used javascript for the redirection. Echoing location.href wont do much on its own! Thanks in advance.
Actually I found this…
echo ‘window.location.href=”‘.home_url().’/page”;’;
Which works great, however there should probably be a NoScript option with a manual link to catch people who have javascript turned off.
Don’t work!!!i have always Warning: Cannot modify header information – headers already sent by (output started at C:\Apache2.2\htdocs\avis.it\wp-admin\menu-header.php:78) in C:\Apache2.2\htdocs\avis.it\wp-includes\pluggable.php on line 881
this is a fake solution!
Thanks so much! I racked my brains over this for ages and found your link way down in the google search results; your is the *only* viable solution for redirecting nicely from within a wp_options page. NICE ;-)
You really save my life ;-)
Works really fine. Thank you so much for this help !