javascript 如何更改联系表 7 动态重定向 URL - WordPress
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16454548/
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 contact form 7 Redirecting URL dynamically - WordPress
提问by Anam
I am building a website for one of my clients and they want a function into their website some thing like the following:
我正在为我的一个客户建立一个网站,他们希望在他们的网站中加入一个类似以下的功能:
when people click the download link, a form will appear ( contact form 7) and after visitors put their details , it will re-directed to the download link.
当人们点击下载链接时,会出现一个表格(联系表格 7),访问者输入他们的详细信息后,它将重定向到下载链接。
I able to re-direct to a new page after form submission by using following additional setting to the contact form 7.
通过使用以下联系表单 7 的附加设置,我可以在表单提交后重定向到新页面。
on_sent_ok: "location = 'http://example.com/';"
However, They have 10 files , I need to change the re-direction link 10 times to trigger the download for the appropriate file. I can do it by using 10 contact forms which will be very dirty.
但是,他们有 10 个文件,我需要更改重定向链接 10 次才能触发相应文件的下载。我可以通过使用 10 个非常脏的联系表格来做到这一点。
Is there any way i can change the re-direction URL dynamically?
有什么办法可以动态更改重定向 URL?
For example,
例如,
http://example.com/?id=1
http://example.com/?id=2
<?php
$id = $_GET['id'];
$url= "http://example.com/id=?". $id;
?>
is there any way to change the following Location with $url ?
有没有办法用 $url 更改以下位置?
on_sent_ok: "location = 'http://example.com/';"
回答by Anam
I have found a way to change the redirection URL dynamically. I have followed the following steps to achieve the dynamic redirection:
我找到了一种动态更改重定向 URL 的方法。我按照以下步骤来实现动态重定向:
In contact form 7's Additional setting put the following:
on_sent_ok: 'redirect();'
We need a hidden field to carry a piece of necessary information. However, Contact form 7 by default do not allow us to create hidden fields. The developer SevenSpark has developed an extension to allow hidden fields in Contact form 7. http://wordpress.org/extend/plugins/contact-form-7-dynamic-text-extension/please download the plugin and install. you will see two new tags have been generated for the contact form 7. this will allow you to grab the value from $_GET variable. please check the details on plugin page.
ex. http://example.com/?foo="bar"
Create a template page or exiting page template ok.
assign the template to the appropriate page. if you want to use the default template you do not need to create or assign any template.
open your template file in an Editor.
paste the following code:
<script> function redirect() { // your hidden field ID var filename = document.getElementById('redFilename').value; var url =''; //alert(document.getElementById('redFilename').value); if (filename == 'apple') { url= 'http://example.com/thankyou/'; } else if (filename == 'orange') { url= 'http://example.com/thankyou_orange/'; } window.location = url; } </script>
Now browse the link with GET parameter.
ex. http://example.com/?redFilename="apple"
在联系表格 7 的附加设置中输入以下内容:
on_sent_ok: '重定向();'
我们需要一个隐藏字段来携带一条必要的信息。但是,默认情况下,联系表单 7 不允许我们创建隐藏字段。开发商 SevenSpark 开发了一个扩展,允许在联系表单 7 中隐藏字段。 http://wordpress.org/extend/plugins/contact-form-7-dynamic-text-extension/请下载插件并安装。您将看到为联系表单 7 生成了两个新标签。这将允许您从 $_GET 变量中获取值。请检查插件页面上的详细信息。
创建模板页面或退出页面模板确定。
将模板分配到适当的页面。如果要使用默认模板,则无需创建或分配任何模板。
在编辑器中打开您的模板文件。
粘贴以下代码:
<script> function redirect() { // your hidden field ID var filename = document.getElementById('redFilename').value; var url =''; //alert(document.getElementById('redFilename').value); if (filename == 'apple') { url= 'http://example.com/thankyou/'; } else if (filename == 'orange') { url= 'http://example.com/thankyou_orange/'; } window.location = url; } </script>
现在浏览带有 GET 参数的链接。
the hidden field of the contact form 7 will capture the redFilename value. if form submission is successful, it will redirect to the http://example.com/thankyou_orange/page
联系表单 7 的隐藏字段将捕获 redFilename 值。如果表单提交成功,它将重定向到http://example.com/thankyou_orange/页面
Enjoy!!!!
享受!!!!
回答by Mohamed Rihan
add_action('wpcf7_mail_sent', 'ip_wpcf7_mail_sent');
function ip_wpcf7_mail_sent($wpcf7)
{
$on_sent_ok = $wpcf7->additional_setting('ip_on_sent_ok', false);
if (is_array($on_sent_ok) && count($on_sent_ok) > 0)
{
wp_redirect(trim($on_sent_ok[0]));
exit;
}
}
回答by Sherif SALEH
on_sent_ok:
never worked for me.
on_sent_ok:
从来没有对我来说有效过。
I tried something like this for tracking conversation.
我尝试过这样的事情来跟踪对话。
var sent = $('#wpcf7-f3224-o1').find('.wpcf7-mail-sent-ok');
if ( sent.length ) {
<?php
$page_name = get_the_title();
$the_name = str_replace(' ', '', $page_name);
?>
self.location="/merci/?page=<?php echo $the_name ?>";
};
回答by Ashar Zafar
Contact form 7 Redirecting to Another URL After Submissions 2017 Updates
联系表格 7 提交后重定向到另一个 URL 2017 更新
First you need to update contact form 7 on new version i try it on v7.4.9 and then place a contact form short code in any page and place this JS script any where on page and change url where need to redirect your page after submission
首先,您需要在新版本上更新联系表单 7,我在 v7.4.9 上尝试,然后在任何页面中放置一个联系表单短代码,并将此 JS 脚本放置在页面上的任何位置,并更改提交后需要重定向页面的 url
<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
location = 'http://example.com/';
}, false );
</script>
For more info click contact form 7 official site https://contactform7.com/redirecting-to-another-url-after-submissions/
欲了解更多信息,请点击联系表格 7 官方网站 https://contactform7.com/redirecting-to-another-url-after-submissions/
回答by icc97
I went down the Javascript route, which works... but you then don't have access to the submitted variables (at least I couldn't see how you would). Here I have ported my Javascript code across to PHP code.
我沿着 Javascript 路线走下去,这行得通……但是你无法访问提交的变量(至少我看不出你会怎么做)。在这里,我已将我的 Javascript 代码移植到 PHP 代码中。
You can access any hidden or displayed inputs in the form and use them to build the URL.
您可以访问表单中任何隐藏或显示的输入并使用它们来构建 URL。
There is one required trick to do the redirect in PHP rather than Javascript, which is that you have to turn off the CF7 Javascript as per the doc. Put this in your wp-config.php
:
在 PHP 而不是 Javascript 中进行重定向需要一个技巧,那就是您必须按照 doc 关闭 CF7 Javascript。把它放在你的wp-config.php
:
define('WPCF7_LOAD_JS', false);
Then you can put this in your theme functions.php
:
然后你可以把它放在你的主题中functions.php
:
add_action( 'wpcf7_mail_sent', 'icc97_so_mail_sent', 10, 3);
/**
* Ported Javascript code to redirect the CF7 form
*
* Have to do it in PHP because we want access to the POSTed data
*
* There is a further complication that the default CF7 submission is AJAX
* Then our WP redirect doesn't do anything
* So we have to turn off the CF7 Javascript via wp-config.php
*
*/
function icc97_so_mail_sent( $contact_form ) {
$submission = WPCF7_Submission::get_instance();
$data = $submission->get_posted_data();
// example data:
// {"_wpcf7":"11684","_wpcf7_version":"4.9","_wpcf7_locale":"en_GB","_wpcf7_unit_tag":"wpcf7-f11684-p11681-o1","_wpcf7_container_post":"11681","your-name":"Ian","your-organisation":"Stack Overflow","your-email":"[email protected]","your-agreement":"1"}
/**
* Get an attribute value from the CF7 form
*
* @param string $name attribute name
* @return string attribute value
*/
$attr = function($name) use ($data) {
$val = $data[$name];
// Dropdown / select values are arrays could be a one element array
return is_array($val) ? $val[0] : $val;
};
/**
* Create URL for downloads page
*
* @param string $domain e.g. https://example.com, can be blank for relative
* @param array $names attributes
* @return string URL e.g. https://example.com/downloads/x/x/x/?data=xxx
*/
$buildUrl = function ($domain, $names) use ($attr) {
$stub = [$domain, 'downloads'];
// we want lower case, attributes
$path = array_map('strtolower', array_map($attr, $names));
// create an array of the full URL and then join with '/'
return join('/', array_merge($stub, $path));
};
$domain = '';
// this requires AJAX to be turned off - see function doc block
\wp_redirect($buildUrl($domain, ['your-name', 'your-organisation']));
// exit otherwise CF7 forces it's own redirect back to the original page
exit;
}