WordPress.org

Ready to get started?Download WordPress

Forums

Contact Form 7
How to: Redirect after submit (23 posts)

  1. maryisead
    Member
    Posted 2 years ago #

    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.

  2. derek_tgm
    Member
    Posted 1 year ago #

    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.

  3. waterworks2
    Member
    Posted 1 year ago #

    Thanks for posting this!!!

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

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

  5. HarrisITServices
    Member
    Posted 1 year ago #

    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;
    }
  6. a_johnson
    Member
    Posted 1 year ago #

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

  7. nina.martinez
    Member
    Posted 1 year ago #

    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!

    :)

  8. maryisead
    Member
    Posted 1 year ago #

    @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).

  9. Mohi69
    Member
    Posted 1 year ago #

    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 ?

  10. maryisead
    Member
    Posted 1 year ago #

    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;
    	}
    }
  11. Mohi69
    Member
    Posted 1 year ago #

    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.

  12. Mohi69
    Member
    Posted 1 year ago #

    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

  13. maryisead
    Member
    Posted 1 year ago #

    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.

  14. Mohi69
    Member
    Posted 1 year ago #

    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

  15. maryisead
    Member
    Posted 1 year ago #

    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);
  16. Mohi69
    Member
    Posted 1 year ago #

    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;
    	}
    }
  17. maryisead
    Member
    Posted 1 year ago #

    Looks fine to me. Any errors shown?

  18. Mohi69
    Member
    Posted 1 year ago #

    No just a white page on all the website online and "wp_redirect" or "Location" doesn't work on local..

  19. maryisead
    Member
    Posted 1 year ago #

    Just tried the exact same code here, works like a charme.

    If you just get a blank page, you should figure out where your error logs are and see what the problem is.

  20. Mohi69
    Member
    Posted 1 year ago #

    It's working perfectly now.. Maybe I was tired yesterday. Thank you very much for all.

  21. maryisead
    Member
    Posted 1 year ago #

    Glad to hear you got it working!

  22. sidralsideral
    Member
    Posted 1 year ago #

    Great, brilliant, superb. Seriously, that was awesome! A million thanks. I've been all night looking for a hack like this.

    I'm using Contact Form 7 on a jQuery Nyromodal popup and I wasn't able to redirect to an specific page after clicking send. The problem was that when I clicked send I was always being redirect to the .php page (which incluedes de CF7) that I loaded with Nyromodal.

    By the moment this hack -targeting a desired page- is a good way to solve that problem. But...

    Does anyone know how to make Contact Form 7 working in a jQuery pop up window without any kind of redirection?

    Thanks

  23. benread
    Member
    Posted 1 year ago #

    I'd like to move from a form submission to another page based on the post category, but can't seem to get this to work within the funcitons.php page. I have multiple pages using the same contact form, and each is tagged with a category. Based on which page they submit the form from, I want to redirect them to a category-specific "next" page. Here's the code I have - any suggestions would be appreciated!

    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);
    
    	global $wp_query;
    	$post = $wp_query->post;
    	$category = get_the_category($post->ID);
    	$thisCatName = $category[0]->category_nicename;
    	$redirectPage = "next-" . $thisCatName . ".html";
    
    	if (is_array($on_sent_ok) && count($on_sent_ok) > 0)
    	{
    		wp_redirect(trim($redirectPage));
    		exit;
    	}
    }

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic