wordpress 如何更改联系表单 7 的表单操作 url?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14177844/
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
how to change form action url for contact form 7?
提问by user1102824
I'm using Contact Form 7 in a wordpress site with multiple forms. I need to direct one form to a different form action url than the others.
我在具有多种表单的 wordpress 站点中使用联系表 7。我需要将一个表单定向到与其他表单不同的表单操作 url。
I found the reply below for a previous thread but I'm not sure how to go about it. Can someone specify what exact code needs to be included in "additional settings" and what the code in functions.php would look like?
我在下面找到了上一个线程的回复,但我不知道如何去做。有人可以指定需要在“附加设置”中包含哪些确切代码以及functions.php 中的代码是什么样的吗?
Thanks for your help!
谢谢你的帮助!
reply from diff. thread, which I don't completely understand...
来自差异的回复。线程,我不完全理解......
*Yes, you have to change the "action" attribute in the form using this Filter Hook wpcf7_form_action_url. (what would be the code?)You could add the hook into your theme's functions.php and then just process the form data in your ASP page.(code?)*
*是的,您必须使用此过滤器挂钩 wpcf7_form_action_url 更改表单中的“操作”属性。(代码是什么?)您可以将钩子添加到主题的functions.php 中,然后在您的ASP 页面中处理表单数据。(代码?)*
回答by maiorano84
Since you're not familiar with PHP code at all, I'll give you a bit of a crash course in coding within the Wordpress API.
由于您根本不熟悉 PHP 代码,因此我将为您提供一些有关 Wordpress API 中编码的速成课程。
First off, you need to know the difference between functions and variables. A variable is a single entity that is meant to represent an arbitrary value. The value can be anything. A number, somebody's name, or complex data.
首先,您需要了解函数和变量之间的区别。变量是表示任意值的单个实体。值可以是任何东西。一个数字、某人的名字或复杂的数据。
A function is something that executes a series of actions to either send back - or return- a variable, or alter a given variable.
函数是执行一系列操作以发送回或返回变量或更改给定变量的东西。
<?php
$a = 1; //Number
$b = 'b'; //String *note the quotes around it*
$c = my_function(); //Call to a function called my_function
echo $a; //1
echo $b; //b
echo $c; //oh, hello
function my_function()
{
return 'oh, hello';
}
?>
Wordpress utilizes its own action and filter system loosely based on the Event-Driven Programmingstyle.
Wordpress 基于事件驱动编程风格松散地利用自己的动作和过滤系统。
What this means is that Wordpress is "listening" for a certain event to happen, and when it does, it executes a function attached to that event (also known as a callback). These are the "Actions" and "Filters". So what's the difference?
这意味着 Wordpress 正在“侦听”某个事件是否发生,当它发生时,它会执行附加到该事件的函数(也称为回调)。这些是“操作”和“过滤器”。那么有什么区别呢?
Actions are functions that do stuff
Filters are functions that return stuff
动作是做东西
的函数过滤器是返回东西的函数
So how does this all fit in to your problem?
那么这一切如何解决您的问题呢?
Contact Form 7 has its own filter that returns the URL of where information is to be sent by its forms.
联系表 7 有自己的过滤器,可返回其表单将信息发送到何处的 URL。
So lets look at the basics of a Filter Hook
那么让我们来看看Filter Hook的基础知识
add_filter('hook_name', 'your_filter');
- add_filter is the function that tells Wordpress it needs to listen for a particular event.
- 'hook_name' is the event Wordpress is listening for.
- 'your_filter' is the function - or callback - that is called when the 'hook_name' event is fired.
- add_filter 是告诉 Wordpress 它需要监听特定事件的函数。
- 'hook_name' 是 Wordpress 正在侦听的事件。
- 'your_filter' 是在触发 'hook_name' 事件时调用的函数 - 或回调。
The link to the previous thread states that the hook name you need to be using is 'wpcf7_form_action_url'. That means that all you have to do is make a call to add_filter, set the 'hook_name' to 'wpcf7_form_action_url', and then set 'your_filter' to the name of the function you'll be setting up as your callback.
上一个线程的链接指出您需要使用的钩子名称是“wpcf7_form_action_url”。这意味着您所要做的就是调用 add_filter,将“hook_name”设置为“wpcf7_form_action_url”,然后将“your_filter”设置为您将设置为回调的函数的名称。
Once that's done, you just need to define a function with a name that matches whatever you put in place of 'your_filter', and just make sure that it returns a URL to modify the form action.
完成后,您只需要定义一个名称与您放置在“your_filter”位置的名称相匹配的函数,并确保它返回一个 URL 以修改表单操作。
Now here comes the problem: This is going to alter ALL of your forms. But first thing's first: See if you can get some working code going on your own. Just write your code in functions.phpand let us know how it turns out.
现在问题来了:这将改变您的所有表单。但首先要做的是:看看您是否可以自己编写一些工作代码。只需在functions.php 中编写您的代码,然后告诉我们结果如何。
UPDATE:
更新:
The fact that you were able to get it so quickly is wonderful, and shows the amount of research effort you're putting into this.
您能够如此迅速地获得它这一事实非常棒,这表明您为此投入了大量的研究工作。
Put all of this in functions.php
把所有这些放在functions.php中
add_filter('wpcf7_form_action_url', 'wpcf7_custom_form_action_url');
function wpcf7_custom_form_action_url()
{
return 'wheretopost.asp';
}
As mentioned before, that will affect ALL of your forms. If this is only supposed to affect a form on a given page, you can do something like this:
如前所述,这将影响您的所有表格。如果这只应该影响给定页面上的表单,您可以执行以下操作:
add_filter('wpcf7_form_action_url', 'wpcf7_custom_form_action_url');
function wpcf7_custom_form_action_url($url)
{
global $post;
$id_to_change = 1;
if($post->ID === $id_to_change)
return 'wheretopost.asp';
else
return $url;
}
All you would need to do is change the value of $id_to_change to a number that represents the ID of the Post/Page you're trying to affect. So if - for example - you have an About Page that you would like to change the Action URL, you can find the ID number of your About Page in the Admin Dashboard (just go to the Page editor and look in your URL for the ID number) and change the 1 to whatever the ID number is.
您需要做的就是将 $id_to_change 的值更改为代表您试图影响的帖子/页面的 ID 的数字。因此,如果 - 例如 - 您有一个关于页面想要更改操作 URL,您可以在管理仪表板中找到您的关于页面的 ID 号(只需转到页面编辑器并在您的 URL 中查找 ID number) 并将 1 更改为任何 ID 号。
Hope this helps you out, and best of luck to you.
希望这能帮到你,祝你好运。
回答by pcatre
You can add actions after a successful submission like the documentation says
您可以在成功提交后添加操作,如文档所述
Adding a filter will work in the sense that it will change the action on the form but unfortunately it will also break the functionality of the plugin. If you add the filter like other answers suggest the form will keep the spinner state after submission.
添加过滤器会改变表单上的操作,但不幸的是它也会破坏插件的功能。如果您像其他答案一样添加过滤器,则建议表单在提交后保持微调状态。
You can make the form do something else on submit by using advanced settings such as:
您可以使用高级设置使表单在提交时执行其他操作,例如:
on_submit: "alert('submit');"
more details about advanced settings here.
有关高级设置的更多详细信息,请访问此处。