• Resolved rod

    (@nomadarod)


    Hi,

    I would like that the form identifies the user’s role, when registered users that are logged in fill the form. That way I could have that parameter in a hidden field that would condition prices.

    I see that a hidden field can fetch that the user ID is not null, but I don’t see a parameter for identifying the user role. Is there one?

    Thanks

    Rod

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Nebu John – WPMU DEV Support

    (@wpmudevsupport14)

    Hi @nomadarod,

    Trust you are doing good and thank you for reaching out to us.

    The following code snippet can help you fetch the user role of a logged-in user to a hidden field.

    <?php
    add_filter( 'forminator_field_hidden_field_value', 'wpmudev_populate_hidden_field_date', 10, 4 );
    function wpmudev_populate_hidden_field_date( $value, $saved_value, $field, $hidden_field ) {
    	if ( 'custom_value' == $saved_value && '{USER_ROLE}' == $value ) {
    		$user_role = '';
    		if ( is_user_logged_in() ) {
    			$user_role = ( ( array ) wp_get_current_user()->roles )[0];
    		}
    		
    		$value = $user_role;
    		
    	}
    	return $value;
    }

    You need to use the custom value as the default value and enter {USER_ROLE} in the value of the hidden field. The code could be added using a mu-plugin, please find more details here: https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    Kindly be aware that there is a known bug causing the Hidden field’s value to not be saved correctly upon form submissions. Nevertheless, the visibility logic based on the field will function properly without any issues.

    I hope that helps.

    Kind Regards,
    Nebu John

    Thread Starter rod

    (@nomadarod)

    Thank you John. I’ll give it a try.

    cheers
    Rod

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @nomadarod,

    Since we haven’t heard from you for a while. I’ll mark this thread as resolved for now. Please feel free to re-open the thread if you need further assistance.

    Kind Regards
    Nithin

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Fetch user role’ is closed to new replies.