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 pages in WPForms (Supports Next and Previous navigation).
 * 
 */
function wpf_dev_skip_empty_pages() {
    ?>
    <script type="text/javascript">
    jQuery(function($) {
        
        // 1. Track the direction the user is trying to go
        var navDirection = 'next'; // Default to moving forward
        // Update direction when Next is clicked
        $(document).on('click', '.wpforms-page-next', function() {
            navDirection = 'next';
        });
        // Update direction when Previous is clicked
        $(document).on('click', '.wpforms-page-prev', function() {
            navDirection = 'prev';
        });
        // 2. The core logic to check the page
        function checkAndSkipCurrentPage($form) {
            // Find the currently visible page container
            var $currentPage = $form.find('.wpforms-page').filter(function() {
                return $(this).css('display') !== 'none';
            });
            // Safety check: if no page is found or we are on the last page (submit page), stop.
            if ($currentPage.length === 0 || $currentPage.hasClass('last')) {
                return;
            }
            var hasVisibleFields = false;
            // Scan for REAL content fields on this page
            $currentPage.find('.wpforms-field').not('.wpforms-field-pagebreak').each(function() {
                var $field = $(this);
                
                // Check 1: Is it hidden by CSS (display:none)?
                var isHiddenByCSS = $field.css('display') === 'none';
                
                // Check 2: Is it hidden by WPForms Conditional Logic?
                var isHiddenByLogic = $field.hasClass('wpforms-conditional-hide');
                
                // Check 3: Is it a hidden field type?
                var isHiddenType = $field.hasClass('wpforms-field-hidden');
                // Check 4: Is it visually invisible (height < 25px)? 
                // This handles the "height: 1px" fields in your specific HTML
                var isTooSmall = $field.outerHeight() < 25;
                // If it passes all checks, we have a real visible field
                if (!isHiddenByCSS && !isHiddenByLogic && !isHiddenType && !isTooSmall) {
                    hasVisibleFields = true;
                    return false; // Stop the loop, we found a field
                }
            });
            // 3. Action: If no visible fields were found
            if (!hasVisibleFields) {
                
                if (navDirection === 'prev') {
                    // If user clicked Previous, trigger Previous on THIS empty page to keep going back
                    var $prevBtn = $currentPage.find('.wpforms-page-prev');
                    if ($prevBtn.length) {
                        $prevBtn.trigger('click');
                    }
                } else {
                    // If user clicked Next (or default), trigger Next to skip forward
                    var $nextBtn = $currentPage.find('.wpforms-page-next');
                    if ($nextBtn.length) {
                        $nextBtn.trigger('click');
                    }
                }
            }
        }
        // Trigger on Page Change
        $(document).on('wpformsPageChange', function(event, pageNum, form) {
            checkAndSkipCurrentPage($(form));
        });
        // Trigger on Conditional Logic Process (in case fields hide while user is staring at the page)
        $(document).on('wpformsProcessConditionals', function(event, form) {
            checkAndSkipCurrentPage($(form));
        });
    });
    </script>
    <?php
}
add_action('wpforms_wp_footer_end', 'wpf_dev_skip_empty_pages', 30);

Comments

Add a Comment