简体   繁体   中英

How to change contact form 7 Redirecting URL dynamically - WordPress

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.

I able to re-direct to a new page after form submission by using following additional setting to the contact form 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.

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

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 ?

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

I have found a way to change the redirection URL dynamically. I have followed the following steps to achieve the dynamic redirection:

  1. In contact form 7's Additional setting put the following:

    on_sent_ok: 'redirect();'

  2. 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"

  3. Create a template page or exiting page template ok.

  4. 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.

  5. open your template file in an Editor.

  6. 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>
  7. Now browse the link with GET parameter.

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

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

Enjoy!!!!

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;
    }
}

on_sent_ok: never worked for me.

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

Contact form 7 Redirecting to Another URL After Submissions 2017 Updates

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

<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/

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.

You can access any hidden or displayed inputs in the form and use them to build the 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 :

define('WPCF7_LOAD_JS', false); 

Then you can put this in your theme 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM