最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Contact Form 7 redirect with on_sent_ok doesn't work? - Stack Overflow

programmeradmin4浏览0评论

I tried using the on_sent_ok to redirect after form submit for Contact Form 7 (Wordpress plugin), but it doesn't work for some reason:

on_sent_ok: "location.replace('/');"

Why wouldn't this work?

I am using CF7 versions 2.2.1 and Wordpress 3.5.0.

I tried using the on_sent_ok to redirect after form submit for Contact Form 7 (Wordpress plugin), but it doesn't work for some reason:

on_sent_ok: "location.replace('http://www.pany./thank-you/');"

Why wouldn't this work?

I am using CF7 versions 2.2.1 and Wordpress 3.5.0.

Share Improve this question edited Jan 7, 2014 at 9:22 Danijel asked Jul 29, 2013 at 9:57 DanijelDanijel 8,61019 gold badges84 silver badges147 bronze badges 1
  • 1 Note that now (2018) on_sent_ok is deprecated – Marco Panichi Commented Dec 19, 2018 at 11:26
Add a ment  | 

8 Answers 8

Reset to default 4

Contact Form 7 Thank You Page Redirects THE OLD METHOD: on_sent_ok hook is no longer remended. I have a code you can copy and paste. You can add that method bottom of your functions.php file, add this code:

<pre>
<code>
add_action( 'wp_footer', 'mycustom_wp_footer' );

function mycustom_wp_footer() {
?>
<script type="text/javascript">
document.addEventListener( 'wpcf7mailsent', function( event ) {
   if ( '97' == event.detail.contactFormId ) { 
    location = 'http://www.vizistata./affordable-logo-designs.php';
    } 
    else if ( '634' == event.detail.contactFormId ) { // 634 is the form id
        location = 'http://www.vizistata./#blog';
    } 

    else if ( '635' == event.detail.contactFormId ) { // 635 is the form id
        location = 'http://www.vizistata./#contact';
    } 

    else if ( '636' == event.detail.contactFormId ) { 
        location = 'http://www.vizistata./#services';
    } 

    else if ( '94' == event.detail.contactFormId ) { 
        location = 'http://www.vizistata./#about';
    } 


    else { // Sends submissions on all unaccounted for forms to the third thank you page
        location = 'http://www.vizistata./';
    }
}, false );
</script>
<?php
}</pre>
</code>

Had the same problem with my own Wordpress theme. After an hour I understood that forgot to add <?php wp_footer(); ?> in footer.php, so important Contact 7 scripts doesn't load. When I add it everthing worked.

on_sent_ok not working in Contact Form 7 normally indicates a Javascript conflict with either your current WordPress theme or one of the other plugins you are using - see Contact Form 7 Email Issues - there is a link there that covers Javascript Conflicts.

Remember that a problem in one script can cause issues in another. For example, some jQuery errors can stop jQuery processing any more script, which can make the culprit hard to find.

A jQuery error in one of your plugins or your themes javascript files can stop jQuery processing before it gets to the CF7 script.

Use Firebug or Chrome Dev Tools to examine Javascript conflicts in detail.

See http://wiki.simple-press./installation/troubleshooting/plugin-conflicts/ for general guidance.

Try this...

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

in additional settings

I've tried both solutions: on_sent_ok didn't work and adding redirect javascript didn't do the job either. Eventually, I found myself editing the file 'submissions.php' inside the 'modules' folder of 'contact-form-7'. look for: private function submit() in there, look for elseif ( $this->mail() I've mented $this->response = $contact_form->message( 'mail_sent_ok' ); and just put header("Location: www.somesite./"); Works pretty well.

Contact form 7 Redirecting to Another URL After Submissions

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./';
}, false );
</script> 

For more info click contact form 7 official site https://contactform7./redirecting-to-another-url-after-submissions/

I faced same problem in contact form 7 latest version 4.8.1 .

finally solved issue using custom jquery code.

if ($('.wpcf7-form.sent').length) {
      $(location).attr('href', 'http://www.example.')
    }

Note: Contact form 7 form successful submit adding class "sent" to form field .

I use the following code for this. I put it in one line to just save a few characters. If necessary, you can still work with is_page or something so that the JavaScript is only output for pages that also contain a form.

/* Inline script for redirect cf7 to thank you page */
// https://contactform7./redirecting-to-another-url-after-submissions/
add_action('wp_footer', 'cf7__redirect_thankyoupage');
function cf7__redirect_thankyoupage() {
    ?><script>document.addEventListener('wpcf7mailsent',function(event) {location='/thank-you';}, false );</script><?php
}

And if you are using WPML and only want to output it for one language, then you can use the following code:

if (null !== ICL_LANGUAGE_CODE && ICL_LANGUAGE_CODE == "en"){ ... }

It is best to set the thank you pages to Noindex (via Robots.txt / Meta).

发布评论

评论列表(0)

  1. 暂无评论