Populate Gravity Form Field with the Current User’s Role

Sometimes when building a Gravity Form, you’ll need to populate the current user’s role into a field. This could be to use with conditional logic, or just for your records.

Whatever the reason, it’s simple to populate the current user’s role into a field using this snippet. The snippet doesn’t require any customization, you just need to enable “Allow field to be populated dynamically” (found under the Advanced tab in the field settings) and enter “user_role” as the Parameter Name:

add_filter( 'gform_field_value_user_role', 'snippetpress_gf_populate_current_user_role' );

function snippetpress_gf_populate_current_user_role( $value ) {
	if( is_user_logged_in() ) {
    	$user = wp_get_current_user();
		$roles = $user->roles;
		$value = implode(', ', $roles);
	return $value;

The great thing about this snippet is, if any of your users have multiple roles assigned to their account, all of the roles will be populated into the field, separated by a comma.

Where to add the snippet?

Whichever snippet you choose to use, you should place it at the bottom of the functions.php file of your child theme. Make sure you know what you’re doing when editing this file. Alternatively, you can use a plugin such as Code Snippets to add the custom code to your WordPress site. If you need further guidance on how to add the code, check out our post on How to Add WordPress Snippets.