Only run gform_pre_render on first page of multipage [RESOLVED]

I’m using gform_pre_render to pull in data to my form from an API and prepopulate fields.
This works great.

Problem: It’s a multipage form, and whenever the user navigates to a new page in the form the API requests are made again, pulling the same data redundantly and slowing everything down.

How can I avoid the API request running more than once?
I’ve played around with adding a css class of ‘updated’ to the form, but this is cleared when the user navigates. I’ve also tried setting a field value in the form to ‘updated’ but don’t know how to read the value during pre_render to avoid it hitting the API.

Any input would be appreciated, thanks guys!

Within your callback function, you can check on the current page with the following:

$current_page = GFFormDisplay::get_current_page( $form['id'] );

Having that, you can then set up an if statement to restrict when your code is run.

2 Likes

That’s a great idea!
It doesn’t solve for the api call happening if the user navigates back/forth but it’s the best solution so far. Thanks!

1 Like