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
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