Inside Gravity Forms documentation , you can find out that you can pass data into Gravity Forms by using any of the following 3 methods: hooks, query strings and shortcodes. Let me show you how to pass data using filter hooks. Basically, what you’re looking for is gform_field_value_$parameter_name() filter. Note the $parameter_name.
The first thing we need is a filter, inside which we put our function that returns some data. There is an example on Gravity Forms website, a quite simple one.
1 2 3 4 5 6 7 |
add_filter('gform_field_value_author_email', 'populate_post_author_email'); function populate_post_author_email($value){ $author_email = get_the_author_meta('email', $post->post_author); return $author_email; } |
The most important thing is to note that $parameter_name should match the returned variable. At least that’s what I think :).
Let me show you another example I created for this purpose. Nothing spectacular here, only the the function that returns the current post title:
1 2 3 4 5 6 7 |
add_filter('gform_field_value_current_post_title', 'get_current_post_title'); function get_current_post_title($the_title){ $current_post_title = get_the_title($post->ID); return $current_post_title; } |
Now, the question is how do we pass this info into Gravity forms, so that our field is populated dynamically? What we need is variable name, but without the $ sign, and you put this into parameter name inside the chosen field:
Or we can do the following (same thing, different variable):
In any case, you can see that variable name must match the parameter name. And that’s all there is – you are free to create your own custom functions.
Finally, this is the result you get in the front – these fields are populated by default when you open the page or post or any other type of custom post type where you put the Gravity form: