Using Gravity Forms ‘gform_confirmation_anchor’ PHP filter

The gform_confirmation_anchor filter in Gravity Forms allows you to control the confirmation anchor functionality that automatically scrolls the page to the confirmation text or validation message upon form submission. It can be set to scroll to a specific position, disable scrolling, or enable it for all forms.

Usage

To apply this filter to all forms, use the following code:

add_filter( 'gform_confirmation_anchor', 'your_function_name' );

To target a specific form, append the form id to the hook name like so:

add_filter( 'gform_confirmation_anchor_5', 'your_function_name' );

Parameters

  • $anchor (bool / int): This parameter indicates how the anchor will behave. By default, it’s true for AJAX enabled or multi-page non-AJAX forms and false for single page non-AJAX forms. Here are the possible settings:
    • true: Page will scroll to the form position.
    • false: Page will not scroll.
    • Any numeric value: Page will scroll to the specified pixel value from the top.
  • $form (Form Object): This parameter represents the current form. It’s available from Gravity Forms 1.9.17.12.

More Information

See Gravity Forms Docs: gform_confirmation_anchor
This filter was added in Gravity Forms version 1.3.13. Ability to pass an integer for $anchor was added in version 1.6. $form parameter was added in version 1.9.17.12. The filter is located in various methods in form_display.php.

Examples

Enable for all forms

This example enables the confirmation anchor on all forms.

add_filter( 'gform_confirmation_anchor', '__return_true' );

Disable for a specific form

This example disables the confirmation anchor for form with ID 5.

add_filter( 'gform_confirmation_anchor_5', '__return_false' );

Set scroll distance

This example causes all AJAX enabled forms to scroll to 20 pixels from the top of the document after being submitted. Only AJAX enabled forms can set the scroll distance.

add_filter( 'gform_confirmation_anchor', function() { return 20; } );

Apply custom function to a specific form

This example applies a custom function to form with ID 10.

function custom_anchor_setting( $anchor, $form ) {
    // your custom code here
    return $anchor;
}
add_filter( 'gform_confirmation_anchor_10', 'custom_anchor_setting', 10, 2 );

Set different scroll distances for different forms

This example sets different scroll distances for form IDs 5 and 10.

function custom_scroll_distance( $anchor, $form ) {
    if ( $form['id'] == 5 ) {
        return 50;
    } elseif ( $form['id'] == 10 ) {
        return 100;
    }
    return $anchor;
}
add_filter( 'gform_confirmation_anchor', 'custom_scroll_distance', 10, 2 );