如何更改联系表单7的表单操作网址? [英] how to change form action url for contact form 7?

查看:127
本文介绍了如何更改联系表单7的表单操作网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在具有多个表单的wordpress网站中使用联系表单7。
我需要将一种表单定向到另一个表单,而不是其他表单。

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.

我在下面找到了上一个线程的答复,但不确定如何去做。
有人可以指定附加设置
中需要包含哪些确切的代码,以及functions.php中的代码是什么样的吗?

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?

谢谢您的帮助!

从diff回复。线程,我不完全理解...

reply from diff. thread, which I don't completely understand...

*是的,您必须使用此过滤器挂钩wpcf7_form_action_url更改表单中的 action属性。 (代码是什么?)您可以将钩子添加到主题的functions.php中,然后仅在ASP页中处理表单数据。(代码?) *

*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?) *

推荐答案

由于您根本不熟悉PHP代码,因此我

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.

首先,您需要了解函数和变量之间的区别。变量是单个实体,旨在表示任意值。该值可以是任何值。数字,某人的名字或复杂的数据。

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根据事件驱动的编程样式。

Wordpress utilizes its own action and filter system loosely based on the Event-Driven Programming style.

这意味着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?

动作是可以执行操作的函数

过滤器是可以返回操作的函数

那么这一切如何适合您的问题?

So how does this all fit in to your problem?

联系表格7包含自己的过滤器,该过滤器返回要通过其表单发送信息的URL。

Contact Form 7 has its own filter that returns the URL of where information is to be sent by its forms.

因此,让我们看一下过滤器挂钩

add_filter('hook_name', 'your_filter');




  1. add_filter是告诉Wordpress需要收听$ b $的函数b表示特定事件。

  2. 'hook_name'是Wordpress正在监听的事件。

  3. 'your_filter'是函数-或回调-

指向上一个线程的链接指出您需要使用的钩子名称是 wpcf7_form_action_url。这意味着您要做的就是调用add_filter,将 hook_name设置为 wpcf7_form_action_url,然后将 your_filter设置为您要设置为回调函数的名称。

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.

完成后,您只需要定义一个名称与您在 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.

现在出现了问题:这将改变您的所有表单。但是首先要考虑的是:看看是否可以自己编写一些工作代码。只需在 functions.php 中编写您的代码,然后让我们知道结果。

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.php and let us know how it turns out.

更新:

您能够如此迅速地获得它的事实真是太棒了,并且表明了您为此付出的努力。

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.

将所有这些都放入 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;
}

所有您需要做的就是将$ id_to_change的值更改为一个数字代表您要影响的帖子/页面的ID。因此,举例来说,如果您有一个要更改操作URL的关于页面,则可以在管理控制台中找到关于页面的ID号(只需转到页面编辑器并在URL中查找ID数字),然后将1更改为ID号。

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.

希望这对您有所帮助,并祝您好运。

Hope this helps you out, and best of luck to you.

这篇关于如何更改联系表单7的表单操作网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆