URL Parameter to hidden fields

[ad_1]

Hi @orjanmen

I hope you’re well today!

I’m guessing you populate that hidden field using some code (probably JS), right?

This would be happening due to the exact reason you want to use it – so user wouldn’t be able to change it. Hidden fields have a number of default options available in their settings and upon submission only those values would be saved. If the field has a different value than one of the values chosen in its settings, such value will not be saved.

Otherwise, user could simply edit the field value in page source or override it using POST request.

—-

You mentioned you fill it with URL parameter. Is it the URL of the page that the form is on? If yes, hidden field has “Embed post/page URL” option available for it that you can use without any code.

If that field is passed over to form via URL, then instead you can use option “Query var” of hidden field to put it in hidden field.

Otherwise, you’ll need to use additional PHP code to keep the field value like this:

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_custom', 10, 3);
function wpmudev_change_hidden_field_data_custom($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'] = 'YOUR CUSTOM VALUE HERE';
        }
    }
}

Note that you need to set your form ID in this code and hidden field ID in two places. Then you’d need to replace YOUR CUSTOM VALUE HERE with either your custom URL or a function that provides it.

If you have any additional questions about it, let us know, please.

Best regards,
Adam

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer