Preselct dropdown menu option by user role [RESOLVED]

Hello,

is it possible to preselect a dropdown field in my form with one of the choices by user role of logged in user? e.g. if user is a subscriber it should select option b by default, if he is contributor option a etc. - User should still be able to change the option. It’s just for experience so user safes himself one click while filling out the form

Thanks in advance!

You can’t do that out of the box. But there’s a filter that you can use to run you own custom code to check the user role and return the value for the choice you want to pre-select: https://docs.gravityforms.com/gform_field_value_parameter_name/

Thank you, this is working for me if anyone else is interested:

add_filter( 'gform_field_value_who', 'pre_select_who' );

function pre_select_who( $value ) {
    if ( is_user_logged_in() ) {
        $current_user = wp_get_current_user();
        $user_roles = $current_user->roles;
		
        if (in_array('subscriber', $user_roles)) {
            return 'User';
        }
		
        elseif (in_array('contributor', $user_roles)) {
            return 'Vendor';
        }
    }

    return $value;
}
1 Like