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

Embed Snippet on Your Site

Skip Page Breaks When Using Conditional Logic

With this snippet, any WPForms form with Conditional Logic and Page Breaks will be skipped automatically.

100+
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($) {
            $('.wpforms-form').each(function() {
                var $form = $(this);
                var action = "";
                var skipCount = 0;
                var maxSkips = 10; // Prevent infinite loops
                $form.on('click', '.wpforms-page-button', function() {
                    action = $(this).data('action');
                    skipCount = 0; // Reset counter on manual navigation
                });
                $form.on('wpformsBeforePageChange', function(event, pageNum, form) {
                    // Only auto-skip when moving forward/backward via buttons
                    if (!action || skipCount >= maxSkips) {
                        return;
                    }
                    setTimeout(function() {
                        var $currentPage = form.find(`.wpforms-page-${pageNum}`);
                        var hasVisibleFields = false;
                        
                        // Check all field containers except the page indicator
                        $currentPage.find('.wpforms-field').each(function() {
                            var $field = $(this);
                            
                            // Check if field is visible and not just whitespace
                            if ($field.is(':visible') && $.trim($field.text()).length > 0) {
                                // Check if it's not just a hidden field or page break
                                if (!$field.hasClass('wpforms-field-pagebreak')) {
                                    hasVisibleFields = true;
                                    return false; // Break the loop
                                }
                            }
                        });
                        // If no visible fields, skip to next/previous page
                        if (!hasVisibleFields && action) {
                            skipCount++;
                            window.wpforms_pageScroll = false;
                            $currentPage.find(`.wpforms-page-${action}`).click();
                        } else {
                            window.wpforms_pageScroll = true;
                            skipCount = 0;
                        }
                    }, 100); // Small delay to ensure conditional logic has processed
                });
            });
        });
    </script>
    <?php
}
add_action('wpforms_wp_footer_end', 'wpf_dev_skip_empty_pages', 30);

Comments

Add a Comment