Skip to content

Instantly share code, notes, and snippets.

@2ndkauboy
Created July 18, 2021 21:07
Show Gist options
  • Save 2ndkauboy/bfa3b0b7623c27548dace6581215dd04 to your computer and use it in GitHub Desktop.
Save 2ndkauboy/bfa3b0b7623c27548dace6581215dd04 to your computer and use it in GitHub Desktop.
Description: Customize the GF progress bar by removing it from the first page
<?php
/**
* GF Progress Bar Customization
*
* @package gf-progress-bar-customization
* @author Bernhard Kau
* @license GPLv3
*
* @wordpress-plugin
* Plugin Name: GF Progress Bar Customization
* Plugin URI: https://gist.github.com/2ndkauboy/bfa3b0b7623c27548dace6581215dd04
* Description: Customize the GF progress bar by removing it from the first page
* Version: 1.0.0
* Author: Bernhard Kau
* Author URI: https://kau-boys.de
* Text Domain: gf-progress-bar-customization
* License: GPLv3
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt
*/
/**
* Check if form is the one we want to customize.
*
* @param object $form The form.
*
* @retun boolean
*/
function gf_pbc_is_customized_form( $form ) {
if ( 'processbar-customized' === $form['cssClass'] ) {
return true;
}
return false;
}
/**
* Check if form progress bar starts at zero.
*
* @param boolean $start_at_zero Value for start.
* @param object $form The form.
*
* @retun object
*/
function gf_pbc_start_at_zero( $start_at_zero, $form ) {
return gf_pbc_is_customized_form( $form );
}
add_filter( 'gform_progressbar_start_at_zero', 'gf_pbc_start_at_zero', 10, 2 );
/**
* Check if form progress bar starts at zero.
*
* @param string $progress_bar The progress bar.
* @param object $form The form.
* @param string $confirmation_message The conformation message.
*
* @retun string
*/
function gf_pbc_change_progressbar_steps( $progress_bar, $form, $confirmation_message ) {
if ( ! gf_pbc_is_customized_form( $form ) ) {
return $progress_bar;
}
$current_page = GFFormDisplay::get_current_page( $form['id'] );
$max_page = GFFormDisplay::get_max_page_number( $form );
if ( 1 === $current_page ) {
$progress_bar = '';
} else {
$progress_bar = preg_replace_callback(
'#<p class="gf_progressbar_title">.*(gf_step_current_page?)(?s).*</p>#',
function ( $matches ) use ( $current_page, $max_page ) {
if ( ! isset( $matches[1] ) ) {
return $matches[0];
}
return '<p class="gf_progressbar_title">' . sprintf(
// translators: %1$d: number of page, %2$d: number of max_pages.
__( 'Question %1$d of %2$d', 'gf-progress-bar-customization' ),
$current_page - 1,
$max_page - 1
) . '</p>';
},
$progress_bar
);
}
return $progress_bar;
}
add_filter( 'gform_progress_bar', 'gf_pbc_change_progressbar_steps', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment