• Resolved dmannouch

    (@dmannouch)


    Hi team,
    (this may also prove useful to those looking for this solution).

    I’ve more recently started disabling jQuery on WordPress in order to ease the amount of resources being loaded on the page.

    As a result, I have since noticed that CF7 seems dependant on jQuery being active in order to recognise the event for re-direction. Because of this, the suggested php function and this plugin does not work in enabling redirection.

    As a feature suggestion, it would be really cool if you guys could add an option for the user to select jQuery or Vanilla re-direction.

    The solution I found is as follows, and works on my website which does not have jQuery called in:

    add_action( 'wp_footer', 'redirect_cf7' );
    
    function redirect_cf7() {
    ?>
        <script>
            document.addEventListener( 'DOMContentLoaded', function() {
                var forms = document.querySelectorAll( 'form.wpcf7-form' );
                Array.prototype.forEach.call(forms, function(form) {
                    form.setAttribute('onsubmit', 'redirectToThankYou();');
                });
            });
    
            function redirectToThankYou() {
                location = '<?php echo home_url(); ?>/thank-you';
            }
        </script>
    <?php
    }
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.