首页 文章

如何更改联系表单7动态重定向URL - WordPress

提问于
浏览
1

我正在为我的一个客户 Build 一个网站,他们想要一个功能进入他们的网站,如下所示:

当人们点击下载链接时,会出现一个表单(联系表单7),在访问者提交详细信息后,它将重新定向到下载链接 .

通过使用以下附加设置到联系表单7,我可以在表单提交后重新指向新页面 .

on_sent_ok: "location = 'http://example.com/';"

enter image description here

但是,他们有10个文件,我需要更改重定向链接10次以触发相应文件的下载 . 我可以使用10个非常脏的联系表格来完成 .

Is there any way i can change the re-direction URL dynamically?

例如,

http://example.com/?id=1
http://example.com/?id=2

<?php

$id = $_GET['id'];

$url= "http://example.com/id=?". $id; 


?>

有没有办法用$ url更改以下位置?

on_sent_ok: "location = 'http://example.com/';"

5 回答

  • 3

    我找到了一种动态更改重定向URL的方法 . 我已按照以下步骤实现动态重定向:

    • 在联系表格7的附加设置中输入以下内容:

    on_sent_ok:'redirect();'

    • 我们需要一个隐藏的字段来携带一条必要的信息 . 但是,默认情况下,联系表单7不允许我们创建隐藏字段 . 开发人员SevenSpark开发了一个扩展,允许在Contact 7中隐藏字段.http://wordpress.org/extend/plugins/contact-form-7-dynamic-text-extension/请下载插件并安装 . 您将看到为联系表单7生成了两个新标签 . 这将允许您从$ _GET变量中获取值 . 请查看插件页面上的详细信息 .

    恩 . http://example.com/?foo= "bar"

    • 创建模板页面或退出页面模板确定 .

    • 将模板分配给适当的页面 . 如果要使用默认模板,则无需创建或分配任何模板 .

    • 在编辑器中打开模板文件 .

    • 粘贴以下代码:

    <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参数的链接 .

    恩 . http://example.com/?redFilename= "apple"

    联系表单7的隐藏字段将捕获redFilename值 . 如果表单提交成功,它将重定向到http://example.com/thankyou_orange/页面

    请享用!!!!

  • 9
    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;
        }
    }
    
  • 0

    联系表单7在提交2017年更新后重定向到另一个URL

    首先,您需要在新版本上更新联系表单7我在v7.4.9上尝试它,然后在任何页面中放置一个联系表单短代码,并将此JS脚本放在页面的任何位置,并在提交后更改需要重定向页面的URL

    <script>
    document.addEventListener( 'wpcf7mailsent', function( event ) {
    location = 'http://example.com/';
    }, false );
    </script>
    

    欲了解更多信息,请点击联系表格7官方网站https://contactform7.com/redirecting-to-another-url-after-submissions/

  • 0

    on_sent_ok: 从未为我工作过 .

    我尝试过类似的东西来跟踪对话 .

    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 ?>";
       };
    
  • 0

    我沿着Javascript路线运行了......但是你没有访问提交的变量(至少我看不到你会怎样) . 在这里,我将我的Javascript代码移植到PHP代码中 .

    您可以访问表单中的任何隐藏或显示的输入,并使用它们来构建URL .

    在PHP而不是Javascript中进行重定向有一个必需的技巧,这是你必须turn off the CF7 Javascript as per the doc . 把它放在_2519018中:

    define('WPCF7_LOAD_JS', false);
    

    然后你可以把它放在你的主题 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":"ian@example.com","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;
    }
    

相关问题