1

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

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
}
?>
chiliNUT
  • 18,989
  • 14
  • 66
  • 106
  • You can always try to put `ob_start();` underneath your first ` – Funk Forty Niner Dec 17 '13 at 04:53
  • 1
    the html that has already been output comes from the wordpress core, not from my page, so output buffering in my own script won't have any effect. I think I need to use a hook but I don't know much beyond that. – chiliNUT Dec 17 '13 at 04:57
  • @chiliNUT why do you want a redirect in manage_articles? So if the user clicks on manage article menu what should it do ? – Sabari Dec 17 '13 at 05:01
  • 1
    You'd have to register an action / hook callback (or whatever Wordpress calls them these days) for one of the early running actions, detect the condition that requires a redirect, then issue the *Location* header and exit. – Phil Dec 17 '13 at 05:06
  • @Sabari I have edited my question to include a use case. – chiliNUT Dec 17 '13 at 05:10
  • @Phil do you know what action to hook into to do that? – chiliNUT Dec 17 '13 at 05:10
  • possible duplicate of [Redircting to a page in PHP](http://stackoverflow.com/questions/19827795/redircting-to-a-page-in-php) – Phil Dec 17 '13 at 05:12
  • Those other questions describe the issue, that I am already aware of, without providing a solution specific to this use case. When I tail my error log I see that **headers already sent by (output started at /var/www/wordpress/wp-admin/menu-header.php:162)** so how can I hook into that? – chiliNUT Dec 17 '13 at 05:16
  • possible duplicate of [Headers already sent by PHP](http://stackoverflow.com/questions/8028957/headers-already-sent-by-php) – Glavić Dec 17 '13 at 21:30
  • Again, I know WHY I am getting the error, the question was WHERE is the initial output being generated so I can execute my header first. – chiliNUT Dec 17 '13 at 21:36

1 Answers1

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). */
//......
chiliNUT
  • 18,989
  • 14
  • 66
  • 106