2

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.

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.

2 Answers 2

2

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.

3
  • +1 You're awesome for adding a solution to an old, seemingly rare issue. The future thanks you, I'm sure.
    – tksb
    Commented Oct 7, 2014 at 18:08
  • Thanks :) I wished this problem (WordPress getting $query_vars not only from GET, but from POST as well) was described somewhere on Codex or somewhere else. That would save me a lot of time and neurons. Hope this will help someone in future. BTW, this question was the only relevant search result in Google. Seems the problem is really rare. And thanks to Otto's comment I understood that my code is 100% fine, the problem lies somewhere else. After only 10 minutes of debugging I figured everything out and fixed it. Commented Oct 8, 2014 at 14:49
  • Getting the same issue, where any inset into post request is 302 found erroring and some settings saved requests are redirecting to the home page. Looking to see what changed and how to fix it. Commented Mar 3, 2015 at 18:10
2

There's nothing wrong with that code. I just did it on a local install and it worked fine, exactly how you'd expect.

Check for conflicting plugins.

1
  • That's part of why this is so irritating, the code is just fine. I'm just hoping for some insight into possible causes, I guess.
    – tksb
    Commented Apr 15, 2011 at 19:22

Your Answer

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

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