• Resolved jamminjames

    (@jamminjames)


    Regarding the Forminator API, I’m using the free version, and are trying to update a hidden field in a form via the API which would then be used to prepopulate the behavior string, but no matter what I try, when it is submitted, the field does not show up in the behavior.

    I want to redirect to a url using a variable, because the url changes. In behavior, I have:

    https://www.forwardto.com/{hidden-4}?type={radio-2}

    The “{radio-2}” variable works fine, as it is entered on the form. However, the {hidden-4} field is the tricky one. It needs to be populated via a variable that is not in the field initially, and I can’t bring it in via a url parameter.

    Using the API, I’m accessing the variable from some userdata (website url). That works fine. Then, I’ve tried these things to update the hidden form field:

    Forminator_API::update_form_field
    Forminator_API::add_form_field

    Looking at the form data with

    Forminator_API::get_form_fields_by_type( $form_id, 'hidden' ) 

    …it looks like the data is being added to the field, but when the form is submitted, it doesn’t get recorded in the db, nor is it used by behavior.

    I’ve also tried Forminator_API::update_form_entry, and that does get recorded in the db, but it apparently happens after the form submission, so it can’t get used in the form ‘behavior’.

    How can this be accomplished? Any help would be appreciated.

    • This topic was modified 1 year, 4 months ago by jamminjames.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Saurabh – WPMU DEV Support

    (@wpmudev-support7)

    Hi @jamminjames

    Hope you are doing fine.

    You can try the following code snippet as an example to change the value of the hidden field. The code will be the following:

    add_filter( 'forminator_prepared_data', 'wpmudev_add_hidden_field_time', 10, 2 );
    function wpmudev_add_hidden_field_time( $prepared_data, $module_object ){
    	if ( $module_object->id != 6 ) {
    		return $prepared_data;
    	}
    
    	$prepared_data['hidden-1'] = $value_of_hidden_field;
    
    	return $prepared_data;
    }
    
    add_action( 'forminator_custom_form_submit_before_set_fields', 'wpmudev_change_hidden_field_date', 10, 3 );
    function wpmudev_change_hidden_field_date( $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-1' ) !== false ) {
                Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = $value_of_hidden_field;
            }
        }
    
    }

    Remember to change the number “6” with your form id.

    Let us know if this code helps. Feel free to reply if you have any additional questions or concerns.

    Kind regards

    Luis

    Thread Starter jamminjames

    (@jamminjames)

    Thanks very much. I couldn’t find anything in the API documentation nor the plugin docs about hooks, other than the depreciated and changed ones it mentions. It only lists methods. Where is the action and filter hook info? It should be easier to find from this API documentation.

    • This reply was modified 1 year, 4 months ago by jamminjames.
    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @jamminjames

    You are right – hooks (both action and filter) currently have no public documentation similar to API methods, I’m afraid.

    I’m not saying that it will never happen but for now I don’t think it’s going to happen soon. It isn’t due to any “secrets” (especially that the code can be checked directly) but mostly due to the fact that fully and clearly documenting all the hooks from all our plugins is a lot, lot of work (by this I mean a proper docs, other than just using automatically generate list of hooks) and it takes a lot of time to go through it and keep up to the changes.

    So for now, you can always do a full-text search in the plugin for “do_action” and “apply_filter” strings and most of them will have at least a short explanation in code comment (or would be more or less self-explanatory).

    I’m aware this may be inconvenient but please also remember that at any time you are more than welcome to just ask us and we’ll be happy to help.

    Best regards,
    Adam

    Thread Starter jamminjames

    (@jamminjames)

    Okay, you’re saying search within the actual plugin files within \wp-content\plugins\forminator\ on our site, yes? Didn’t think of that.

    I hope WPMU can document them at some point.

    Thanks for all your help!

    Plugin Support Zafer – WPMU DEV Support

    (@wpmudevsupport15)

    Hi @jamminjames,

    I hope you are doing well today!

    Okay, you’re saying search within the actual plugin files within \wp-content\plugins\forminator\ on our site, yes??

    Yes, we are referring to text search within plugin files and we hope to have a full documentation in the future when possible.

    Kind regards,
    Zafer

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Using Forminator API to prepopulate field’ is closed to new replies.