Code Snippet
When using Gravity Forms you can decide on a per-form basis as to whether or not you want to automatically scroll to the page top after a form submit.
There are times when a form is towards the bottom of the page and you want the screen to stay in that position so that the user can see the confirmation text. Other times, you may want the page to scroll back to the top because the form is long and the confirmation text will be towards the top.
PHP – Don’t Scroll to the Top
Place the following code anywhere in your theme’s child functions.php document. Make sure to change the forms to the actual one’s you don’t want to scroll. You can list as many forms here as necessary (we are listing two forms).
// don't scroll to the top of page after submit
add_filter( 'gform_confirmation_anchor_1', '__return_false' );
add_filter( 'gform_confirmation_anchor_4', '__return_false' );
Note
Line 2 and line 3 each refer to a different form on your site. They do so by ID number. gform_confirmation_anchor_1 refers to the form with ID 1. gform_confirmation_anchor_4 refers to the form with ID 4. Look at your list of Gravity forms to find their respective IDs.
PHP – Do Scroll to the Top
Place the following code anywhere in your child theme’s functions.php document. These are the forms we want to scroll back to the page top after begin submitted.
// do scroll to the top of page after submit
add_filter( 'gform_confirmation_anchor_2', function() { inquiry
return 0;
} );
add_filter( 'gform_confirmation_anchor_3', function() { inquiry
return 0;
} );
Note
Line 2 and line 5 each refer to a different form on your site. They do so by ID number. gform_confirmation_anchor_2 refers to the form with ID 2. gform_confirmation_anchor_3 refers to the form with ID 3. Look at your list of Gravity forms to find their respective IDs.
Note
All modifications to a theme or plugin should be made by creating a child theme and placing the changes there. Changes made to the parent theme will be overwritten the next time it updates.
WordPress Notes:
- All modifications to a theme or plugin should be made by creating a child theme and placing the changes there; changes made to the parent theme will be overwritten the next time it updates
We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.