bookmark_borderDynamically Populating the Post Author

If you aren’t familiar with dynamic population with Gravity Forms, it is probably worth your while to take a quick breeze through the Using Dynamic Population walk-through.

This walk-through demonstrates how to retrieve the author’s email based on the post that the Gravity Form is being displayed.

Let’s jump right into the code!

add_filter( 'gform_field_value_author_email', 'populate_post_author_email' );
function populate_post_author_email( $value ) {
    global $post;

    $author_email = get_the_author_meta( 'email', $post->post_author );

    return $author_email;
}

The first thing you would do with this code is copy and paste it into your theme’s functions.php file. Now let’s break it down line by line to get a better understanding of what is happening.

Registering a Function to a Hook

add_filter( 'gform_field_value_author_email', 'populate_post_author_email' );

With this line, we are registering a custom function populate_post_author_email to the gform_field_value_$parameter_name filter. All this means is when Gravity Forms is rendering the form on a page it will call our custom populate_post_author_email function *and then* populate the value returned by our custom function into the field.

So how does Gravity Forms know which field to populate with the returned value? The dynamic population parameter! Note that you can apply the same parameter name to as many fields as you’d like and this code would populate them all.

Ready for the next bit of code?

Getting and Returning the Value

The next step is getting the value we want to populate into the field and then returning it so Gravity Forms can populate our field.

global $post;

$author_email = get_the_author_meta( 'email', $post->post_author );

return $author_email;

In this example, we want to get the author’s email based on the post that the Gravity Form is embedded in. To get access to all the juicy post details, we need to declare the $post object global. We can now access various properties of the current post.

One of those properties is the post_author which is the ID of the current post’s author. Why do we need this? To get the author’s email of course! We’ll pass the author’s ID and the author meta (aka ’email’) we are looking for to the WordPress get_the_author_meta function. This function will then return the… you guessed it, author email.

Now all we have to do is return the author’s email back to Gravity Forms which will automatically populate our field with the returned value.

Using ‘Admin Only’ Fields

We’ve got our field populating with the post author’s email perfectly… but then you do your first test submission and realize that the author’s email is included in the {all_fields} token when the email notification is generated. You’d prefer that this field only be visible to the admin. What do you do?

You set the field’s visibility to Administrative and update the merge tag to {all_fields:noadmin}. This will prevent the field from displaying in the default notification emails. If the field has dynamic population enabled, this field will display as a hidden field when the form is displayed. If dynamic population is not enabled, the field will not display at all.

Conclusion

Let us know if you have any questions on this or suggestions to better explain this functionality.