Hello @bewatermyfriend
I trust you’re doing well!
Autopopulating fields so far is possible only with the registered users. It fills in the user information automatically once the form is loaded for logged in user.
If the registered user, on the User Profile has given the information for the Name-1
, Name-2
, Name-3
, it may possible to achieve but will require a custom coding. The following solution was provided by our developer:
If for example, you’re Using XProfile from BuddyPress, and have a custom field called “company name”.
https://monosnap.com/file/WvJpzjI1Zg4wfDKwRz1evxF7RYZ9fq
On a form add d a text field with a custom CSS identifier as “company-name”.
https://monosnap.com/file/5w4rqqBhvvmwu2lUBJI6fbOOVVF2Zw
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.
https://monosnap.com/file/soyvSjHoZeO0nLQCHY3lmcohUPASDP
Hope this helps!
Cheers,
Nastia