最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Passing POST data from one WP post to another

programmeradmin3浏览0评论

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="/" 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?

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/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 edited Jan 2, 2012 at 8:33 Bainternet 67.7k8 gold badges132 silver badges188 bronze badges asked Jan 2, 2012 at 4:26 MichaelMichael 812 silver badges6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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');
发布评论

评论列表(0)

  1. 暂无评论