wordpress 如何在wordpress页面之间传递数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9127054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:52:37  来源:igfitidea点击:

How to pass data between wordpress pages

wordpress

提问by Rajat

I am new to wordpress and i want to transfer data from one page to another in wordpress. I used php to post my form data to a wordpress page. My code is:

我是 wordpress 的新手,我想在 wordpress 中将数据从一个页面传输到另一个页面。我使用 php 将我的表单数据发布到 wordpress 页面。我的代码是:

<form method='post' action='http://www.example.com/www/create_website/'>
.....
<input type ='submit' value = 'OK'/>
</form>

But every time i clicked the submit button i got error page not found. Why it is so because link is loading the page in browser but not working for form post. How can i solve the issue???

但每次我点击提交按钮时,我都找不到错误页面。为什么会这样,因为链接正在浏览器中加载页面,但不适用于表单发布。我该如何解决这个问题???

回答by I_miss_Steve_already

Having spent about a day figuring out how to do this, I thought it might be helpful to others in the stackoverflow community.

花了大约一天的时间弄清楚如何做到这一点后,我认为这可能对 stackoverflow 社区中的其他人有所帮助。

GOAL: To take the results from a form field and use it on another page. In this example, my client wanted users to be able to fill in any amount in a text field to make a Paypal payment. I'm using S2Member plugin to create buttons and hook up the Paypal payments, but they did not offer this functionality --only buttons with hard wired amounts. You can see this in action here:
http://virginiabloom.com/?page_id=250. But it is LIVE. If you start a paypal payment, it WILL deduct money from your account. Which will make Virginia very happy. HOW I SOLVED IT: First, I created a form just for this field. Enter Other Payment Amount $

目标:从表单域中获取结果并在另一个页面上使用它。在此示例中,我的客户希望用户能够在文本字段中填写任何金额以进行 Paypal 付款。我正在使用 S2Member 插件来创建按钮并连接 Paypal 付款,但他们没有提供此功能——只有带有硬连线金额的按钮。你可以在这里看到这个:
http: //virginiabloom.com/?page_id=250。但它是现场直播。如果您开始使用贝宝付款,它将从您的帐户中扣除款项。这会让弗吉尼亚非常高兴。我是如何解决的:首先,我为这个领域创建了一个表单。输入其他付款金额 $

Next, I found a very simple plugin on stackoverflow and adapted it to my needs:

接下来,我在stackoverflow上找到了一个非常简单的插件,并根据我的需要进行了调整:

<?php
/*
Plugin name: redirect on post
Desciption: 

http://stackoverflow.com/questions/13686245/how-to-create-a-custom-url-based-on-dropdown-in-wordpress-form-submission
I-changed-dropdown-to-field-input
*/ 
function redirect_on_submit() {
  // check if the post is set
  if (isset($_POST['amount']) && ! empty ($_POST['amount'])) 

{
    header( "Location: yourUrl.com/?

page_id=542&amount=" . $_POST['amount'] );
  }
}
add_action('init', redirect_on_submit);

NOTE: Change the page URL and ID information in the header to point to the Wordpress page you want to send the information to. If you don't know how to manually install a plugin, its very simple. Save the snippet as a .php file. Open up your hosting account and find the plugins folder (its in the wp-content folder) Copy the file into the folder. Go back to Wordpress and look at your plugins. You should see it there. If its not activated, the activate it.

注意:更改标题中的页面 URL 和 ID 信息以指向您要将信息发送到的 Wordpress 页面。如果您不知道如何手动安装插件,它非常简单。将代码段另存为 .php 文件。打开您的主机帐户并找到插件文件夹(在 wp-content 文件夹中)将文件复制到该文件夹​​中。返回 Wordpress 并查看您的插件。你应该在那里看到它。如果未激活,则激活它。

I installed another plugin: Allow PHP in Posts and Pages (Version 3.0.4)

我安装了另一个插件: Allow PHP in Posts and Pages (Version 3.0.4)

After installing it, the plugin will appear on the Wordpress menu. Open it and you can use their snippet maker that will put php into your post inside a short code. Configuration:
Show the snippet not found message: yes (for debugging) Use the old (pre.2.2 code: no Use the advanced filter method: yes Remove all plugin data on install: no You should see the code snippet box appear at the bottom of the page. My snippet was simple. I just wanted to echo the "amount" parameter from the page url. So it looked like this:

安装后,该插件将出现在 Wordpress 菜单上。打开它,您可以使用他们的代码片段生成器,将 php 放入您的帖子中的短代码中。配置:
显示未找到的片段消息:是(用于调试)使用旧的(2.2 之前的代码:否使用高级过滤器方法:是在安装时删除所有插件数据:否您应该看到代码片段框出现在底部页面的。我的代码段很简单。我只是想从页面 url 中回显“amount”参数。所以它看起来像这样:

echo $_GET['amount']?>

Note that you have to put the ending php tag, but not the beginning tag. Now, you just use the shortcode [php function=1] on the page wherever you want to get this parameter.

请注意,您必须放置结束 php 标签,而不是开始标签。现在,您只需在页面上想要获取此参数的任何位置使用短代码 [php function=1]。

Here's what it looks like on MY results page.

这是我的结果页面上的样子。

<h2>Payment of $ [php function=1]</h2>
<strong><em>Click Paypal button to start payment or cancel.</em></strong>
[s2Member-PayPal-Button sp="1" ids="xxx" exp="24" desc="Payment" ps="paypal" lc="" cc="USD" dg="0" ns="1" custom="yoururl.com"
ra="[php function=1]" image="default" output="button" /]

To make sure that users can only enter in decimal numbers, I used jQuery validation on the form. There are other ways to do it, but this was very simple. Just put this code on the page with the form which I've included below.

为了确保用户只能输入十进制数字,我在表单上使用了 jQuery 验证。还有其他方法可以做到这一点,但这非常简单。只需将此代码放在页面上,并使用我在下面包含的表单。

 <form id="myform" action="http://virginiabloom.com/?page_id=542" method="post"><span style="font-size:1.5em;">Enter Other Payment Amount $</span><input id="amount" class="left" name="amount" type="text" /><input type="submit" value="Submit" /></form>

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://jquery.bassistance.de/validate/jquery.validate.js"></script>
<script src="http://jquery.bassistance.de/validate/additional-methods.js"></script>
<script>
$( "#myform" ).validate({
  rules: {
    amount: {
      required: false,
      number: true
    }
  }
});
</script>

That's it! I hope this helps somebody.

就是这样!我希望这对某人有帮助。

回答by Libin

If you need to go to one of your WordPress page, please don't put the direct link in action. Instead of that put the below php code.

如果您需要访问您的 WordPress 页面之一,请不要使用直接链接。而不是把下面的php代码。

<form method='post' action='<?php bloginfo('url'); ?>/your-page-name/' > 

If you wants to go to a particular file inside your template directory put the below code

如果您想转到模板目录中的特定文件,请输入以下代码

<form method='post' action='<?php bloginfo('template_url'); ?>/your-page.php' > 

Check this WordPress codex page for more about bloginfo.

查看此WordPress 代码页面以了解有关 bloginfo 的更多信息

<?php bloginfo('url'); ?> 

will generate the base site url.

将生成基本站点 url。