Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

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');
share|improve this answer
    
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 '12 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 '12 at 14:56
    
This worked great for me -- thank you! –  Ryan May 29 '13 at 22:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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