Today I had a request from a client who wanted to redirect users who submitted a particular form on their WordPress website to a different page on the site.  The reason for this is they had quite a few people who filled out the very large form, didn’t notice an error and then left thinking it had all worked.

You’d think it’s pretty easy right?  This proved to be a bit more complex than first thought.

Research showed adding this into the “Additional Settings” part of the Contact Form would just do the job:

on_sent_ok: “location.replace(‘http://www.YOURSITE.com’);”

It didn’t.  That led me up the garden path of JQuery installations and references within the site, all of which were fine.

Then I stumbled across this solution, which did the business in a matter of minutes.

Step 1

Add the following to your Theme Functions file (functions.php).  This adds code to the post-mail-sending process of the contact form.

add_action(‘wpcf7_mail_sent’, ‘do_wpcf7_mail_sent’);
function do_wpcf7_mail_sent($wpcf7)
{
$on_sent_ok = $wpcf7->additional_setting(‘on_sent_ok’, false);
if (is_array($on_sent_ok) && count($on_sent_ok) > 0)
{
wp_redirect(trim($on_sent_ok[0]));
exit;
}
}

Step 2
In the contact form – go to the Additional Settings (right down the very end of the page) and add in this line:

on_sent_ok: http://www.theurltoredirect.to

(Naturally you would replace the part in red with the URL you want to redirect to).

Then – head to your contact form page on the website and try it out.  For me it worked first time!