I am new to wordpress and this might be a really simple task. I have a created a form using gravity forms plugin. I need to populate an options field in the form by making an API call. I had implemented this using gform_pre_render action and calling the function to make the API call. This code was in funtions.php of the active theme. This was working as expected.
However, now I'm required to do the same in a plugin file as I cannot have custom code in the themes file. When I place the same code in a plugin file and install the plugin, I am not getting the options field populated by API. Is there any other way to do this? Please let me know. Any help is much appreciated.
You can use the gform_pre_render
hook to add your custom code to the plugin. The only difference is that you need to use the add_filter
function instead of add_action
:
add_filter( 'gform_pre_render', 'populate_options' );
The add_filter
function is the same as add_action
except that it expects a callback function that returns a value. In the case of the gform_pre_render
hook, the callback function should return the form object.
So, the code you want to run when the form is rendered should look something like this:
function populate_options( $form ) {
// your code goes here
return $form;
}