2

I'm trying to pass data between two Wordpress posts via POST method from a form/link. However, the second post does not seem to be able to grab the POST data. I'm wondering if it has anything to do with my permalink structure which has no filenames, only directory paths.

Page 1 uses the following form to "link" to page 2 while sending data through a hidden field via POST Method:

 <form name="offer" action="http://themotoroilevaluator.com/members-blog/motor-oil-bible-special-offer/" method="post" style="padding: 0px; margin: 0px;">
    <input type="hidden" name="discount" value="yes">
    <INPUT TYPE="image" SRC="/members-blog/wp-content/uploads/2011/12/special-offer.png" HEIGHT="350" WIDTH="550" BORDER="0" ALT="Discount Plus Additional Special Bonus Downloads - Click Here">
</form>

Then, on page 2 I have the following php code which is supposed to grab the data from that hidden field from the POST array:

$discount = $_POST['discount'];

Should be simple enough, but it's not working. I tried placing the exact same code on a standalone php page and had the form post to that page as the "action", and it worked fine. What do I need to do to get this to work in Wordpress?

echoing $discount or $_POST['discount'] yields nothing and print_r($_POST) yields an empty array. Any thoughts?

1 Answer 1

2

You need to register the query var so it doesn't get stripped by WP. Add this to your functions.php file.

function foo_add_query_var($vars) {
    $vars[] = 'discount';
    return $vars;
}
add_filter('query_vars', 'foo_add_query_var');

To call this in your template, simply use the following:

$discount = get_query_var('discount');
2
  • Brian, thanks for your help. Unfortunately, this did not work. I added the code indicated to my functions.php file for my theme (also tried adding it to the custom_functions.php file instead). Then, I placed the "$discount =" code within my post (which is setup to be able to execute php code), but the data is still not passing to this page. echoing the $discount variable yields nothing.
    – Michael
    Jan 2, 2012 at 14:44
  • Is it because I'm using POST method instead of GET? Seems like I've found other examples similar to your code on other sites, but they all seem to be related to grabbing query strings from the URL. Or am I missing something?
    – Michael
    Jan 2, 2012 at 14:56

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.