• Resolved chelminski

    (@chelminski)


    Hi. I would like to ask you to add a function to limit the visibility of fields in the form depending on the role. There are two options:

    1. This may be implemented as a new feature.
    2. Add a parameter to “hidden field”. Parameter: “All user roles”

    How will this help? I will be able to set the rule: ” Show if ‘hidden-field’ contains the value ‘author’ “

    To better explain what I mean, I am attaching photos.

    1. Add hidden field to form

    2. Set a hidden field to display all user roles (prototype)

    3. In the block you want to hide or show depending on the role, set rules.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello @chelminski !

    Hope you’re having a good day!

    While this is not an out of the box feature of Forminator at the moment, we have a snippet to achieve this exact scenario:

    <?php
    
    add_filter('forminator_field_hidden_field_value', 'add_userrole_forminator_hidden_field', 10, 4);
    function add_userrole_forminator_hidden_field($value, $saved_value, $field, $hidden_field)
    {
        $user_role = '';
        if (is_user_logged_in()) {
            $user_role = implode(',', (array) wp_get_current_user()->roles);
        }
    
        return str_replace('{USER_ROLE}', $user_role, $value);
    }
    
    add_filter('forminator_prepared_data', 'wpmudev_update_hidden_field_val_copy', 10, 2);
    function wpmudev_update_hidden_field_val_copy($prepared_data, $module_object)
    {
        $form_ids = array(1671); //Please change the form ID
        if (!in_array($module_object->id, $form_ids)) {
            return $prepared_data;
        }
    
        foreach ($prepared_data as $key => $value) {
            if (strpos($key, 'hidden-1') !== false) {
                $prepared_data[$key] = sanitize_text_field($_POST[$key]);
            }
        }
    
        return $prepared_data;
    }
    
    add_action('forminator_custom_form_submit_before_set_fields', 'wpmudev_change_hidden_field_data_userrole', 10, 3);
    function wpmudev_change_hidden_field_data_userrole($entry, $module_id, $field_data_array)
    {
        $form_ids = array(1671); //Please change the form ID
        if (!in_array($module_id, $form_ids)) {
            return;
        }
    
        foreach ($field_data_array as $key => $value) {
            if (strpos($value['name'], 'hidden-1') !== false) {
                Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = sanitize_text_field($_POST[$value['name']]);
            }
        }
    }
    

    To install the snippet, please do the following:

    • copy the code to a .php file in wp-content/mu-plugins/ on your site
    • change the $form_ids = array(1671); to match your form ID, which you can find in the URL of the form on its edit screen or in the shortcode, you can add multiple IDs this way by separating those with ,
    • add a new Hidden field to the form(s), set it to Custom Value and in the Value put {USER_ROLE} – this will be replaced by the actual rowl
    • adjust all occurrences of hidden-1 in the code in case your forms have it with a different ID number
    • set visibility rules using the Contains option like you showed on the screenshot
    • test

    I just tested it on my test site and it worked perfectly.

    Kind regards,

    Pawe?

    Thread Starter chelminski

    (@chelminski)

    Thank you very much! It works!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Restricted block’ is closed to new replies.