0

I have no idea what I'm doing wrong, but I can't get the wp_redirect to actually redirect the page. I enabled the wp_debug to check for "header already sent" errors, but nothing. I installed the "Better HTTP Redirects" plugin to check the response, but it says 302 found.

If I check the wp_redirect response like this, it says "redirect success". But nothing really happens in the end, no errors show up either.

if ( wp_redirect( admin_url( 'admin.php?page=plugin_test&success=item_saved' ) ) ) {
   echo 'redirect success'; 
} else {
   echo 'no';
}

I tried to add ob_start to the init, and add an action for the admin_init and call ob_start from there but both made no difference. This is a stripped down version of the code I have, but it has the same problem.

What happens in the complete code is that the user fills in a form, adds the item and it is saved to the db. After that the user is redirected and a custom msg is shown. The whole script works fine, except for the last redirect.

class Submit_Test {

    function __construct() {
        add_action( 'init', array( $this, 'init' ) );
    }

    public function init() {
        add_action( 'admin_menu', array( $this, 'create_admin_menu' ) );    
    }

    public function create_admin_menu() {           
        add_menu_page( 'Plugin test', 'Menu', 'manage_options', 'plugin_test', array( $this, 'item_list' ) );
    }

    public function item_list() {
        if ( isset( $_REQUEST['actions'] ) ) {
            $this->add_item();
        } 

        if ( isset( $_GET['success'] ) ) {
            $this->show_success_notifications();
        }

        if ( $_GET['action']['add_item'] ) { 
            ?>
                <form method="post" action="" accept-charset="utf-8">
                    <input type="submit" name="actions" class="button-primary" value="add_item" />
                </form>
            <?php
        } else {
            ?>
                <ul>
                    <li><a href="http://localhost/wp-latest/wp-admin/admin.php?page=plugin_test">Current Item</a></li>
                    <li><a href="http://localhost/wp-latest/wp-admin/admin.php?page=plugin_test&action=add_item">Add Item</a></li>
                </ul>
            <?php
        }   
    }

    public function add_item() {
        if ( wp_redirect( admin_url( 'admin.php?page=plugin_test&success=item_saved' ) ) ) {
            echo 'redirect success';    
        } else {
            echo 'no';
        }

         exit();
    }

    public function show_success_notifications() {  
        echo 'yes'; 
    }   
}

new Submit_Test;

Any ideas on how to get the redirect to work, and what i'm doing wrong? I'm out of ideas on what could be the problem here.

6
  • 2
    your item_list function will certainly run after output has already started, so this does seem like a headers already sent issue.
    – Milo
    Nov 18, 2013 at 21:58
  • note that headers already sent is a warning and not a fatal error, so you may have the wrong error reporting level and you're just not seeing it.
    – Milo
    Nov 18, 2013 at 22:02
  • wp_redirect() is a pluggable function, so a plugin or a theme may have overridden it. Try disabling all your plugins and switch to a default theme like Twenty Twelve or Twenty Thirteen, and then see if the problem persists. (Admittedly, it's a long shot, but at the very least you'll be able to eliminate your plugins as the culprit.)
    – Pat J
    Nov 19, 2013 at 4:44
  • Is using header('Location: '.$newURL); an option?
    – Steven
    Nov 19, 2013 at 8:05
  • It turned out to be a "headers already sent" issue after all. I only saw the message after disabling all other plugins. Not exactly sure why it happens, but I found that the slideshow gallery plugin that I use (wordpress.org/plugins/slideshow-gallery) is preventing the "header already sent" from appearing and the redirect from working. At least when I disable it, it works fine. I searched in the slideshow gallery code for 'wp_redirect', to I see if it's being used or modified. But it returns no results. How else could another plugin break / prevent the wp_redirect from working?
    – tijmen
    Nov 19, 2013 at 11:13

2 Answers 2

1

wp_redirect() does not exit automatically and should almost always be followed by exit.

Have you tried adding an exit after wp_redirect? It could be as simple as that

1
  • Yeah, the reason dijmen is doing an echo after wp_redirect and actually seeing the output sort of proves this theory :) One should always exit or die() after a redirect.
    – kovshenin
    Mar 28, 2015 at 10:36
0

You are hooking in too late to use wp_redirect, your code isn't being executed until the admin_menu hook fires, which is after headers have already been sent to the browser and therefore too late to redirect the user.

Create a new function that fires on the admin_init hook and listen for your action there to redirect.

public function init() {
    add_action( 'admin_menu', array( $this, 'create_admin_menu' ) );
    add_action( 'admin_init', array( $this, 'do_actions' ) ); 
}

public function do_actions() {
    if ( isset( $_REQUEST['actions'] ) && $_REQUEST['actions'] == 'MY_UNIQUE_ACTION_NAME' ) {
        $this->add_item();
    } 
}
1
  • Actually admin_menu menu fires before admin_init ;) Both actions are executed before any response is sent and both are fine for a redirect header.
    – kovshenin
    Mar 28, 2015 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.