Support » Plugin: Contact Form 7 » [Plugin: Contact Form 7] How to: Redirect after submit

  • Just wanted to share how I do my redirects with Contact Form 7 as the official solution to this isn’t very satisfying for a number of reasons (especially the dependance on JavaScript).

    I’d love to put this in an already existing thread here, but they’re all closed already.

    Anyway! No source hacks involved, it relies on an action added by CF7 itself, no JavaScript needed.

    In your theme’s function.php (or in a mini plugin, whatever) add this code:

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

    The ip_ is a prefix I’m using. You can change the identifiers if you know your stuff.

    Then, in any CF7 form in the additional settings (at the very bottom), add this:
    ip_on_sent_ok: http://example.com

    Does the trick for me.

Viewing 15 replies - 1 through 15 (of 22 total)
  • Man, I couldn’t thank you enough for this!!!!! I’ve been googling nonstop for 5-6 hours to find a solution, ’cause nothing else worked for me.

    Thanks for posting this!!!

    Do you mean ‘functions.php’ ? I only have a functions.php in my theme.

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    As mawe indicated, if you don’t want to put that code into your theme’s functions.php file then creating a small plugin isn’t that difficult.

    Create a text file in wp-content/plugins named cf7-redirect-after-submit.php or something shorter and put these lines in it:

    <?php
    /*
    Plugin Name: CF7 Redirect after Submit
    Version: 0.1
    */

    And paste the code from above right after that. The visit your plugins page and activate the plugin you created called CF7 Redirect after Submit.

    If anything goes wrong just delete that file via FTP or whatever file management tools your host provided you with.

    Any idea why this wouldn’t be working for me? I have tried the following two snippets of code in functions.php and the email sends fine, but the page never redirects. It just stays with the little spinning circle next to the submit button. Any help would be greatly appreciated as I would like to get this working without using JavaScript to redirect.

    add_action('wpcf7_mail_sent', 'wpcf7_redirect_on_submit');
    function wpcf7_redirect_on_submit($wpcf7)
    {
    		wp_redirect( 'http://www.google.com' );
    		exit;
    }
    add_action('wpcf7_mail_sent', 'wpcf7_redirect_on_submit');
    function wpcf7_redirect_on_submit($wpcf7)
    {
    		header( 'Location: http://www.google.com' );
    		exit;
    }

    Awesome! Thank you. This should be a feature of CF-7!

    Thank you so much for this snippet; however, I too am only receiving a spinning circle (but am also receiving the emails).

    I tried to implement this snippet through my functions.php and it didn’t work; so, I tried implementing it via creation of a plugin.

    All seems well, except I’m not being redirected to my http://example.com/thank-you page.

    Any help would be greatly appreciated!

    🙂

    Thread Starter maryisdead

    (@mawe)

    @waterworks2: Yes, of course, functions.php. My bad!

    @harrisitservices, @nina.martinez: It seems like you’re using the Ajax submit stuff that CF7 uses by default. I forgot to mention that my solution only works when this is disabled, sorry! My workaround relies on a “real” roundtrip to the server.

    I don’t like the JavaScript and CSS stuff CF7 provides very much and removed it at some point with the following code in wp-config.php:

    define('WPCF7_LOAD_CSS', false); // Removes CF7 CSS
    define('WPCF7_LOAD_JS', false); // Removes all CF7 related JavaScript and thus disables the Ajax submitting
    define('WPCF7_AUTOP', false); // Disables putting frickin' paragraphs around everything

    If you’re using that, you will have to go with the official way (which should then just work fine).

    Hi,

    thank you very much.

    On my contact form I ask the user mail. Is it possible to pass this parameter to the new page ?

    Thread Starter maryisdead

    (@mawe)

    Hey, Mohi69!

    Have you tried inspecting the callback parameter that gets passed?

    Something along the lines of this:

    add_action('wpcf7_mail_sent', 'ip_wpcf7_mail_sent');
    function ip_wpcf7_mail_sent($wpcf7)
    {
    	var_dump($wpcf7);
    	die();
    
    	$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;
    	}
    }

    Maybe it has the stuff you need. You could then do the redirect like this (untested code, I’m guessing the properties here):

    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]) . '?email=' . $wpcf7->form_vars['user-email']);
    		exit;
    	}
    }

    It’s not working. I got this : /?email=#wpcf7-f5857-p6021-o1

    Otherwise I tried to add an additional settings (emailTest: <[your-email]>) and get it like this :
    $emailTest = $wpcf7->additional_setting(‘$emailTest’, false);

    I got this : /?email=Array#wpcf7-f5857-p6021-o1

    Do you have an other idea ?

    Thank you.

    I found a solution but the @ desapear. For instance if the mail is example@test.com I will get : /?email=exampletest.com#wpcf7-f5857-p6021-o1

    Do you know why ?

    thank you again

    Thread Starter maryisdead

    (@mawe)

    It would’ve been helpful if you’d have posted your code, mate. 🙂

    Anyways, I just took a look at one of my installations and you’ll get your fields this way:

    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))
    	{
    		wp_redirect(trim($on_sent_ok[0]) . '?email=' . $wpcf7->posted_data['user-email']);
    		exit;
    	}
    }

    See that $wpcf7->posted_data thing? It seems to hold all the information the user submitted with the form.

    It’s working but I still got the same problem. The @ disappears :
    My mail is : aa@bb.cc and I get :
    ?email=aabb.cc#wpcf7-f5857-p6021-o1

    Thread Starter maryisdead

    (@mawe)

    Then I think wp_redirect is doing some non-sense sanitizing on the URL.

    You could try this line instead of wp_redirect:

    header("Location: " . trim($on_sent_ok[0]) . '?email=' . $wpcf7->posted_data['user-email'], true, 302);

    I tried this but my website doesn’t work anymore with :

    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))
    	{
    header("Location: " . trim($on_sent_ok[0]) . '?email=' . $wpcf7->posted_data['your-email'], true, 302);
    
    		exit;
    	}
    }
Viewing 15 replies - 1 through 15 (of 22 total)
  • The topic ‘[Plugin: Contact Form 7] How to: Redirect after submit’ is closed to new replies.