• Resolved danielstoetter

    (@danielstoetter)


    Hi, I need to auto fill a hidden field with a stored cookie value. Is there a way to do this in forminator? Do you have a function or any script to achieve this? Thank you for your support!

Viewing 8 replies - 16 through 23 (of 23 total)
  • Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @danielstoetter

    I got feedback from our developers and the issue on homepage seems to be related to a longstanding WordPress core issue.

    A bit “dirty trick” (but seems to be working fine) would be to use GET request instead. A modified code would be:

    <?php 
    
    add_action( 'wp', 'wpmudev_forminator_custom_query_var_get' );
    function wpmudev_forminator_custom_query_var_get() {
        $_POST['wpmuhfcv_param1'] = !empty( $_GET['ref_id'] ) ? $_GET['ref_id'] : 'novalue';
        $_POST['wpmuhfcv_param2'] = !empty( $_GET['another_query'] ) ? $_GET['another_query'] : 'novalue';
    }
    
    add_filter( 'forminator_field_hidden_field_value', 'wpmudev_populate_hidden_field_cookie_value', 99, 4 );
    
    function wpmudev_populate_hidden_field_cookie_value( $value, $saved_value, $field, $hidden_field ) {
    
        if ( 'custom_value' == $saved_value && '{cookie_val1}' == $value ) {
            if ( $_POST['wpmuhfcv_param1'] <> 'novalue' ) {
                $cookieValue = $_POST['wpmuhfcv_param1'];
            }
            else {
                $cookieValue = $_COOKIE['MyCookieParam1'];
            }
            $value = $cookieValue;
        }
    
        if ( 'custom_value' == $saved_value && '{cookie_val2}' == $value ) {
            if ( $_POST['wpmuhfcv_param2'] <> 'novalue' ) {
                $cookieValue = $_POST['wpmuhfcv_param2'];
            }
            else {
                $cookieValue = $_COOKIE['MyCookieParam2'];
            }
            $value = $cookieValue;
        }
        
        return $value;
    }

    Note that you need to adjust it a bit again to set your own URL parameter names and your cookie names. URL parameters would be in $_GET in these lines

    $_POST['wpmuhfcv_param1'] = !empty( $_GET['ref_id'] ) ? $_GET['ref_id'] : 'novalue';
        $_POST['wpmuhfcv_param2'] = !empty( $_GET['another_query'] ) ? $_GET['another_query'] : 'novalue';

    and cookie names in these lines

    $cookieValue = $_COOKIE['MyCookieParam1'];
    $cookieValue = $_COOKIE['MyCookieParam2'];

    Best regards,
    Adam

    Thread Starter danielstoetter

    (@danielstoetter)

    Thank you very much! This works perfectly now! Hope the “dirtyness” is not a problem as long as it works.

    Again: Great support!

    Thanks!

    Thread Starter danielstoetter

    (@danielstoetter)

    Hi! I have to come back to this once more. I work with this code and it is working great. But now I tried to send data to Make with the webhook and realized, that the data in the hidden field will not be send with the webhook, instead {cookie_val1} is sent as the value. Also in the entries I can only see {cookie_val1}, not the real value. The hidden field is correctly filled and as I was sending data with another integration it was working, but with the webhook not.

    Is there any idea for a solution to this?

    Thank you!
    Daniel

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @danielstoetter,

    I’m checking with our developer regarding this and will get back to you once we get further feedback asap.

    Kind Regards,

    Nithin

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @danielstoetter,

    Please try the following updated snippet and then check whether it helps:

    <?php
    
    add_filter( 'forminator_prepared_data', 'wpmudev_update_hidden_field_val', 10, 2 );
    function wpmudev_update_hidden_field_val( $prepared_data, $module_object ){
        $form_ids = array(6); //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-' ) !== 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', 10, 3 );
    function wpmudev_change_hidden_field_data( $entry, $module_id, $field_data_array ) {
        $form_ids = array(6); //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-' ) !== false ) {
                Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = sanitize_text_field( $_POST[$value['name']] );
            }
        }
    
    }
    

    Please do make sure to update the reference of 6 in the above snippet, with your form ID.

    The given code can be added as a mu-plugins as instructed before. Please check this link on how to implement the above code as a mu-plugins:
    https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    Best Regards,

    Nithin

    Thread Starter danielstoetter

    (@danielstoetter)

    Thank you very much for this code. It works! If I need it for multiple forms, do I have to enter all form IDs with comma? Or would it be not also OK, to have a code for all forms?

    Thanks!
    Daniel

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @danielstoetter

    Yes, listing all form ID’s would be fine.

    You have two copies of this line in the code:

    $form_ids = array(6); //Please change the form ID

    so you’d need to list the same IDs in both, for example:

    $form_ids = array(6,12,57); //Please change the form ID

    would make this solution work with forms of IDs: 6, 12 and 57.

    If you want it to work for all the forms instead (existing and added in future) without any exceptions, you can modify it by simply removing both occurences of this code block:

     $form_ids = array(6); //Please change the form ID
    	if ( !in_array( $module_object->id, $form_ids ) ) {
    		return $prepared_data;
    	}

    Best regards,
    Adam

    Thread Starter danielstoetter

    (@danielstoetter)

    Great! It works perfectly!

    Thanks for the amazing support!
    Daniel

Viewing 8 replies - 16 through 23 (of 23 total)
  • The topic ‘Auto fill hidden field with cookie value’ is closed to new replies.