Phantom 302 status code when sending a POST requests on pages

I really hope I’ve got a simple issue in front of me here, as it’s sort of driving me up a wall.

Short story: submitting a simple form on the front end of the site results in a 302 Found redirect, totally stripping the $_POST data in the process.

Read More

Scenario:

  • Create a page in the admin called Edit
  • Create a custom template file (ie, my-edit-template.php) and apply it to Edit

my-edit-template.php:

<?php
/**
 * Template Name: My Edit Template
 */

if ($_POST['action']) {
var_dump($_POST);
} 

get_header(); ?>

<form action="" method="post">
    <input name="my-ornery-field" value="">
    <input type="submit" value="submit">
    <input type="hidden" id="action" name="action" value="update-profile">
</form>

<?php get_footer(); ?>

Now, when I submit the form, I get two requests to the same page, first, the POST, with the form data intact, and second, a GET (status code 200), obviously stripped of the $_POST global, or anything useful to me.

I’m sure this is a total oversight, but I’m at a loss; utterly confused.

I’ve tried:

  • All plugins disabled
  • Flushing rewrites back to basic defaults
  • POSTing via Javascript/XHR

Help?

I’m sorry if the details are sparse, I’m sort of in class right now, but I’ll follow-up with anything else relevant, later.

Related posts

Leave a Reply

2 comments

  1. This question is pretty old but the only one that popped out for this kind of problem. I had it this night, spend almost 4 hours hitting myself against the wall – disabling plugins, commenting code here and there (and this is a large multisite project with lots of custom functionality and frontend forms, custom urls and templates etc.). That was a pain…

    I will just leave it here. Maybe it will save someone couple of hours.

    When building custom frontend forms, always prepend your
    input/select/etc names with your custom prefix
    .”

    When you forget about this and start naming fields to fit your global naming convention, you can create a select which shows your custom taxonomy and name it the same as a custom taxonomy is named itself. When you hit “Submit”, WordPress receives your $_POST data, parses it, “understands” this name as its registered $query_var and transforms the request, adding this $query_var. Of course it results in a redirect, clearing your $_POST data and preventing form from doing what it supposed to do.