Home / Admin / Hiding Page Breaks When Using Conditional Logic
Duplicate Snippet

Embed Snippet on Your Site

Hiding Page Breaks When Using Conditional Logic

This snippet will hide pages in your form that include fields hidden with conditional logic if the condition is not met.

<10
Code Preview
php
<?php
/**
 * Skip empty page breaks if the condition is not met
 *
 * @link https://wpforms.com/developers/how-to-skip-page-breaks-when-using-conditional-logic/
 */
function wpf_dev_skip_empty_pages() {
    ?>
    <script type="text/javascript">
        jQuery(function($) {
            var action = "";
            $(document).on('click', '.wpforms-page-button', function() {
                action = $(this).data('action');
            });
            $('.wpforms-form').each(function() {
                $(this).on('wpformsBeforePageChange', function(event, pageNum, form) {
                    setTimeout(function() {
                        var emptyPage = true;
                        
                        form.find(`.wpforms-page-${pageNum} > div:not(:last)`).each(function() {
                            var field = $(this);
                            if (field.is(':visible')) {
                                var hasInput = field.find('input, select, textarea').filter(function() {
                                    return $(this).val().trim() !== '';
                                }).length > 0;
                                if (hasInput) {
                                    emptyPage = false;
                                    window.wpforms_pageScroll = true;
                                    return false;
                                }
                            }
                        });
                        if (emptyPage) {
                            window.wpforms_pageScroll = false;
                            form.find(`.wpforms-page-${pageNum} .wpforms-page-${action}`).click();
                        }
                    }, 0);
                });
            });
        });
    </script>
    <?php
}
add_action('wpforms_wp_footer_end', 'wpf_dev_skip_empty_pages', 30);

Comments

Add a Comment