Categories: Wordpress

WordPress and wp_redirect() function problem

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:


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.

if (isset($_POST))
	$error_found = FALSE;

	//	Some input field checking

	if ($error_found == FALSE)
		//	Use the wp redirect function
		//	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&amp;noheader=true">
		<p class="submit">
	    	<input type="submit" class="button-primary" value="Submit form" />

Simple, right? Enjoy ;)


  1. 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.

  2. 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.


    <a href="options-general.php?page=my-first&id=&delete_status=1″ > Delete

  3. Hi Dev,

    Can you post a big more of the PHP code?
    That HTML code you posted is missing “&noheader=true”

  4. 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

    //NEW LINE: using javascript AFTER page has fully rendered :-)
    echo ‘location.href=”‘.get_permalink($firstchild->ID).'”‘;

  5. Hi, Can you explain further how you used javascript for the redirection. Echoing location.href wont do much on its own! Thanks in advance.

  6. 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.

  7. Don’t work!!!i have always Warning: Cannot modify header information – headers already sent by (output started at C:\Apache2.2\htdocs\\wp-admin\menu-header.php:78) in C:\Apache2.2\htdocs\\wp-includes\pluggable.php on line 881

    this is a fake solution!

    1. (output started at C:\Apache2.2\htdocs\\wp-admin\menu-header.php:78) in C:\Apache2.2\htdocs\\wp-includes\pluggable.php on line 881 i have same error with my plugin you can simply put ob_start() at the start of your page and at the end of page add line ob_end_flush(), this works for me.

      Hope this helps!!

  8. 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 ;-)

Leave a Reply

Your email address will not be published. Required fields are marked *