Skip to content

Instantly share code, notes, and snippets.

@n7studios
Created November 19, 2015 16:14
Show Gist options
  • Save n7studios/f0b3ce229fa686ea0184 to your computer and use it in GitHub Desktop.
Save n7studios/f0b3ce229fa686ea0184 to your computer and use it in GitHub Desktop.
Gravity Forms - Move Progress Bar to Bottom of Form
<?php
/**
* Plugin Name: Gravity Forms: Move Progress Bar to Bottom of Form
* Plugin URI: http://www.n7studios.co.uk
* Version: 1.0.0
* Author: n7 Studios
* Author URI: http://www.n7studios.co.uk
* Description: Moves the progress bar from the top to the bottom of the Gravity Form. The Start Paging section of the form MUST have a CSS class = progress-bar-bottom
*/
/**
* Moves the progress bar from the top to the bottom of the Gravity Form
* if the Start Paging section of the form has a CSS class = progress-bar-bottom
*
* @since 1.0.0
*
* @param string $form_string Form HTML
* @param array $form Gravity Form Configuration
* @return string Form HTML
*/
function gravity_forms_move_progress_bar( $form_string, $form ) {
// Check if Pagination is enabled on this form
if ( ! is_array( $form['pagination'] ) ) {
return $form_string;
}
if ( empty( $form['pagination']['type'] ) ) {
return $form_string;
}
// Check if the first page CSS class is progress-bar-bottom
if ( ! isset( $form['firstPageCssClass'] ) ) {
return $form_string;
}
if ( $form['firstPageCssClass'] != 'progress-bar-bottom' ) {
return $form_string;
}
// If here, the progress bar needs to be at the end of the form
// Load form string into DOMDocument
$dom = new DOMDocument;
@$dom->loadHTML( $form_string );
// Load Xpath
$xpath = new DOMXPath( $dom );
// Find Progress Bar
$progress_bar = $xpath->query( '//div[@class="gf_progressbar_wrapper"]' )->item(0);
// Find Form
$form = $xpath->query( '//form' )->item(0);
// Move Progress Bar to end of the Form
$form->appendChild( $progress_bar );
// Get HTML string
$form_string = $dom->saveHTML();
// Return modified HTML string
return $form_string;
}
add_filter( 'gform_get_form_filter', array( $this, 'gravity_forms_move_progress_bar' ), 10, 3 );
Copy link

ghost commented Nov 29, 2016

Is there anything that would keep this from working in a multi-site environment?

@endymion0000
Copy link

Activating this plugin causes all of my forms to not appear regardless of pagination or progress-bar-bottom class being used and generates an error in the log:

PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, first array member is not a valid class name or object in wp-includes/class-wp-hook.php on line 300

Using Wordpress 4.7.3, Gravity Forms 2.2.1, PHP 7.0

@tasos-ch
Copy link

Thank you for the DOM related code that performs the move.

@zacharycaplan
Copy link

FWIW if you change line 63 to the following, it works as intended:

add_filter( 'gform_get_form_filter', 'gravity_forms_move_progress_bar', 10, 3 );

@decaySmash
Copy link

If you have more than one class in your form here then you will need to change line 35:

if ( $form['firstPageCssClass'] != 'progress-bar-bottom' ) {

        return $form_string;

   }

to something like this:

if ( strpos($form['firstPageCssClass'], 'progress-bar-bottom' ) === false) {

        return $form_string;

    }

@TP-Banks
Copy link

@decaySmash thank you for your comment

In my case works, when changing line 35 to

    if ( strpos($form['firstPageCssClass'], 'progress-bar-bottom' ) !== false) {
        return $form_string;
    }

@robbertvancaem
Copy link

robbertvancaem commented Jan 14, 2025

Just came across this piece of code, and noticed that a description had an issue with character encoding. I fixed it by updating the snippet to the one below. Please note that I have also removed the check for the specific class; in my case it wasn't needed

Thanks for the gist anyway!

/**
 * Move progress bar to the bottom
 */
function gravity_forms_move_progress_bar($form_string, $form)
{
  // Check if Pagination is enabled on this form
  if (! is_array($form['pagination'])) {
    return $form_string;
  }
  if (empty($form['pagination']['type'])) {
    return $form_string;
  }

  // If here, the progress bar needs to be at the end of the form
  // Load form string into DOMDocument with proper UTF-8 encoding
  $dom = new DOMDocument('1.0', 'UTF-8');
  // Properly handle UTF-8 input
  $form_string = mb_convert_encoding($form_string, 'HTML-ENTITIES', 'UTF-8');

  // Disable libxml errors and use error handling
  $previous = libxml_use_internal_errors(true);
  $dom->loadHTML($form_string, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
  libxml_clear_errors();
  libxml_use_internal_errors($previous);

  // Load Xpath
  $xpath = new DOMXPath($dom);

  // Find Progress Bar
  $progress_bar = $xpath->query('//div[@class="gf_progressbar_wrapper"]')->item(0);

  // Find Form
  $form = $xpath->query('//form')->item(0);

  // Move Progress Bar to end of the Form
  if ($progress_bar && $form) {
    $form->appendChild($progress_bar);
  }

  // Get HTML string while preserving UTF-8 encoding
  $form_string = $dom->saveHTML();

  // Return modified HTML string
  return $form_string;
}

add_filter('gform_get_form_filter', 'gravity_forms_move_progress_bar', 10, 3);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment