Gravity Forms - Get Current Page Number

The OPs accepted answer might well work but it doesn't work if you have the form setup to paginate using Ajax.

In that case I could only get it working using Javascript and the following method;

http://www.gravityhelp.com/documentation/page/Gform_post_render

jQuery(document).bind('gform_post_render', function (event, formId, current_page) {
    if (current_page == 2) {
        // do something
    }
});

You could of course use the formId parameter to limit this to a specific form


The currently accepted answer only works if the user never goes to a previous page in the form, if they do, then gform_sourge_page_number always lags by one. I found a better solution (using this hook for an example, but you should be able to use it within any hook that has the $form passed to it):

function run_script_on_last_page( $form)  {
  if ( !is_admin() ) {
    if ( \GFFormDisplay::get_current_page( $form['id'] ) == 10) {
        wp_enqueue_script( 'custom_script', get_template_directory_uri() . '/js/custom_script.js', array( 'jquery' ), null, true );
    }
  }
}

add_action( 'gform_enqueue_scripts_63', 'run_script_on_last_page' );

GFFormDisplay::get_current_page( $form_id ) is one of many handy undocumented functions.


I wrote a little function that returns the current page:

// Get Gravity Forms Current Page
// Syntax: gf_current_page()
function gf_get_current_page()
{
    return rgpost('gform_source_page_number_' . $_POST['gform_submit']) ? rgpost('gform_target_page_number_' . $_POST['gform_submit']) : 1;
}

I got it.

The function rgpost is, apparently, crucial in accessing the current page number. After some muddling around on my own, I was able to get the following code working in both functions.php and just before the wp_head() function in header.php.

function run_script_on_last_page($form) {
    if (!is_admin()) {
        $current_page = rgpost('gform_source_page_number_' . $form['id']) ? rgpost('gform_source_page_number_' . $form['id']) : 1;
        if ($current_page == 10) {
            wp_enqueue_script('custom_script', get_template_directory_uri() . '/js/custom_script.js', array('jquery'), null, true);
        }
    }
}
add_action('gform_enqueue_scripts_63', 'run_script_on_last_page');

If you're copy/pasting the code above, make sure to:

  • replace 10 with the page you want to check
  • ensure your parameters are correct in wp_enqueue_script
  • replace 63 with your form ID

Some resources I found useful:

  • this section of the documentation for the gform_validation filter
  • the documentation for gform_enqueue_scripts.