[Forminator Pro] Autofill - add custom field

Hello
How can I add more fields that are stored in the database and use them as autofill? Similar to the already suggested fields like Display Name, First name, ....

  • Kris
    • Support

    Hi Reinhard

    Hope you are doing good today.

    I ping our SLS Team (code experts) so that they can check if this will be possible. We will post an update here as soon as more information is available.

    Please keep in mind that our SLS Team deals with more complicated issues, so it may take a little longer for them to reply here. Thank you for your patience while we look into this further.

    Kind Regards,
    Kris

  • Konstantinos Xenos
    • Rubber Duck Debugger

    Hey Reinhard ,

    Sorry for the late reply here.

    You can add a hidden field with a customized value like so:

    And customize this code accordingly to "replace" that value with any you like:

    <?php
    
    add_filter(
    	'forminator_field_hidden_field_value',
    	function( $value, $saved_value, $field, $obj ) {
    		if ( '%my_value_1%' === $value ) {
    			$value = 'a new value';
    		}
    
    		return $value;
    	},
    	99,
    	4
    );

    The code should be added as a mu-plugin and as an example it could be added like this:
    Always make sure to keep a backup of your site before changing/adding custom code.

    1] Navigate to your /wp-content/ directory and create a new one named mu-plugins if it doesn't exist.
    2] Inside the mu-plugins folder create a file named frmt-custom-hidden-values.php
    3] Edit the file and copy / paste the code snippet.
    4] Save and close the file.
    5] The final path should look like /wp-content/mu-plugins/frmt-custom-hidden-values.php

    If you tell me what fields would you need from your database so I could replicate a setup or test it on your end, I'd be able to help you customize this even more for you to help as much as possible, the above code is just a simple working example for the time being with a static value change :slight_smile: .

    Regards,
    Konstantinos

  • Konstantinos Xenos
    • Rubber Duck Debugger

    Hey Reinhard ,

    You can update the code with something like this:

    <?php
    
    add_filter(
    	'forminator_field_hidden_field_value',
    	function( $value, $saved_value, $field, $obj ) {
    		if ( '%my_value_1%' === $value ) {
    			if ( is_user_logged_in() ) {
    				$company = get_user_meta( get_current_user_id(), 'company', true );
    
    				if ( ! empty( $company ) ) {
    					$value = $company;
    				} else {
    					$value = '';
    				}
    			} else {
    				$value = '';
    			}
    		}
    
    		return $value;
    	},
    	99,
    	4
    );

    I've added a couple of checks since you need a value that should exist for "logged in" users only. So there are 2x "else" clauses in there to alter that text in any case of the value being empty or if the user is not logged in at all. You can remove them though depending on how your form is actually set up ( i.e. only for logged in users ).

    I hope this helps!

    Regards,
    Konstantinos

  • Konstantinos Xenos
    • Rubber Duck Debugger

    Hey Reinhard ,

    As I've mentioned on my first comment, this isn't an 'Autofill' field, it's an actual Hidden Field instead that you can add in your form to pass any custom data in it :slight_smile: .

    If you want a "normal field" i.e. being actually visible to the user please tell me so I can update my example.

    On the other hand if you need more fields like this with extra values you can simply add more Hidden Fields and adding extra "custom values" like %my_value_2%, %my_value_3% and updating the code accordingly to fill them.

    Regards,
    Konstantinos

  • Konstantinos Xenos
    • Rubber Duck Debugger

    Hey Reinhard ,

    Let's give it a try this way then!

    You can insert an Input Field in your form

    And add the "%my_value_1%" as it's default value like so:

    Then the updated code would be :

    <?php
    
    add_filter(
    	'forminator_field_markup',
    	function( $html ) {
    		if ( is_user_logged_in() ) {
    			$company = get_user_meta( get_current_user_id(), 'company', true );
    
    			if ( ! empty( $company ) ) {
    				$html = str_replace( '%my_value_1%', $company, $html );
    			}
    		} else {
    			$html = str_replace( '%my_value_1%', '', $html );
    		}
    
    		return $html;
    	},
    	99
    );

    This will fetch the company name from the database again and "replace" the my_value_1 with the meta value that was found :slight_smile: .

    In a similar manner you can add more input fields as long as you keep the "my_value_" identifiers unique and update the code accordingly.

    Regards,
    Konstantinos

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.