How can I set a php header redirect in a wordpress admin page

EDIT: I have expanded the example code to include a basic use case.

I am trying to make an admin page for a plugin in wordpress. I have succeeded in making a simple admin page using the instructions in the codex. However, I am currently stuck. I am trying to use a

Read More
header("Location: /myLocation");

call, but I can’t set a header since apparantly by the time the admin page has been loaded, other HTML has already been output. I want to use a header redirect so that the form handler I am building for the admin page can use a POST to GET pattern. (postback the form to itself with post, process the post data, header redirect back to itself with get, so it prevents reposts and form resubmission warnings)

Is there some way I can modify my set up so that I am able to make a call to header()?

I followed the tutorial here
http://codex.wordpress.org/Adding_Administration_Menus

and here is my code, which is currently in a mu-plugin. Will making it a regular plugin have any affect on my ability to use header()? I’d rather stick with a mu-plugin if possible but would use a regular one if I had to.

Here’s the code, everything works fine except the call to header(). Its just the sample given in the tutorial with some of my own text put in.

Thanks for reading!

<?php
/** Step 2 (from text above). */
add_action('admin_menu', 'my_plugin_menu');

/** Step 1. */
function my_plugin_menu() {
    add_menu_page('My Plugin Options', 'Manage Articles', 'manage_options', 'manageArticles', 'manage_articles');
}

/** Step 3. */
function manage_articles() {
    if (!current_user_can('manage_options')) {
        wp_die(__('You do not have sufficient permissions to access this page.'));
    }
?>

<?php

//this block of code is an example of what I am trying to do

if (isset($_POST['formSubmit']) && $_POST['formSubmit']==='true')
{
global $wpdb;
$wpdb->query("UPDATE myTable set name='{$_POST['something']}' where id='{$_POST['id']}'");
header("Location: manageArticles?updated");
}
if (isset($_GET['updated']))
{
echo "you have updated the table!";
}

    ?>
    My admin page
    <form name="theForm" action="" method="post">
        Something: <input type="text" value="something" name="something"><br>
        Row ID: <input type="text" value="1" name="id"><br>
        <input type="hidden" value="true" name="formSubmit">
        <input type="submit">
    </form>
    <?php
}
?>

Related posts

Leave a Reply

1 comment

  1. So… I still have more to learn about hooks. Since this is an mu-plugin, according to the list of admin hooks here, I can use the muplugins_loaded hook

    So put this at the top of the plugin

    <?php
    add_action('muplugins_loaded', 'my_plugin_override');
    
    function my_plugin_override() {
        // your code here
        if (isset($_POST['formSubmit']) && $_POST['formSubmit'] === 'true') {
            global $wpdb;
            $wpdb->query("UPDATE myTable set name='{$_POST['something']}' where id='{$_POST['id']}'");
            header("Location: manageArticles?updated");
        }
        if (isset($_GET['updated'])) {
            echo "you have updated the table!";
        }
    }
    ?>
    
    <?php
    //the rest of the code...
    /** Step 2 (from text above). */
    //......