[Forminator Pro] Auto Populate

Can I pre-populagte a form with customer information from BuddyPress?

  • Ohidul Islam
    • Staff

    Hello idomagic,

    Thank you for coming to our support, hope you are well today!

    Right now, pre populating forms with customer information from BuddyPress feature isn't available. But it sounds like a good feature to have in Forminator. So I've made a feature request to our developers. When there's new update for this feature we will notify you.

    Let me know if you have any more questions. Have a nice day!

    Kind regards,
    Ohid

  • Konstantinos Xenos
    • Rubber Duck Debugger

    Hey idomagic ,

    Since I don't know exactly how your Forminator form or xProfile fields are set I can only give you an example, but it is really easy to follow.

    Let's start with the setup in my example:

    I have an xProfile filed called "company name".

    And on my form I have added a text field with a custom css identifier as "company-name".

    Now to get the xProfile field value we can utilize the bp_get_profile_field_data() that BuddyPress provides. To do that we will make a mu-plugin like so:

    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-bp-fields.php
    3] Edit the file and copy / paste this code snippet inside.

    <?php
    
    add_action(
    	'wp_footer',
    	function() {
    		// we only run this code if the user is logged in.
    		if ( is_user_logged_in() ) {
    
    			// we retrieve the fields data from BuddyPress.
    			$company_name = bp_get_profile_field_data(
    				array(
    					'field'   => 'company name',
    					'user_id' => get_current_user_id(),
    				)
    			);
    
    			?>
    			<script>
    			( function( $ ) {
    				$( document ).ready( function() {
    					// we add the data into the value of the input field.
    					$( '.company-name input' ).val( '<?php echo $company_name; ?>' );
    				} );
    			} ( jQuery ) );
    			</script>
    			<?php
    		}
    	},
    	999
    );

    4] Save and close the file.
    5] The final path should look like /wp-content/mu-plugins/frmt-bp-fields.php

    In the code mentioned above as you see we have to get the fields value first by doing

    $company_name = bp_get_profile_field_data(
    	array(
    		'field'   => 'company name',
    		'user_id' => get_current_user_id(),
    	)
    );

    You can change the "company name" and $company_name depending on the field names that you have to easily update the code and you can add as many as you like.

    Next we simply add the value that we got from BuddyPress to the field of the form like so:
    $( '.company-name input' ).val( '<?php echo $company_name; ?>' );

    Again this code will have to be adjusted so it can work depending on your forms custom field classes etc.

    As a result for this example I'm getting a text field automatically populated with "wpmu dev" as that's the company name on my xProfile field as well.

    Tell me if you need any further help with this!

    Regards,
    Konstantinos

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.