[Forminator Pro] customer user field into forminator

I am creating a support form on my website for users that are logged in. I would like basic fields to be automatically populated, such as Name, Email, Phone number, Business Name.

Name/Email/Phone number are easy because they are standard WP fields and Forminator uses those easily.

But my Business name field is a customer field created by memberpress “mepr_business_name”

I tried to follow the instructions on this forum post https://wpmudev.com/forums/topic/forminator-pro-i-need-to-know-how-i-can-pass-values-into-a-form/#post-3869222 However that refers to custom fields for pages, not users.

Could you tell me how to insert a custom user field into a forminator field when a users is logged in please?

Thank you

  • Nebu John
    • FLS

    Hi tracikay ,

    Trust you are doing good and thank you for reaching out to us.

    Could you please try using the following workaround and check if that helps?

    <?php
    add_filter( 'forminator_field_create_input', function( $html, $attr ){
    	static $list_fields = [];
    	if( ! $list_fields ){
    		
    		// Get the ID of the currently logged-in user
     		$user_id = get_current_user_id();
    
    		// Replace 'mepr_business_name' with the specific meta key you want to retrieve
    		$meta_key = 'mepr_business_name';
    
    		// Fetch the user meta
    		$user_meta = get_user_meta( $user_id, $meta_key, true );
    		
    		$list_fields = array(
    			'text-1' => $user_meta,
    		);
    		
    	}
    	if( $list_fields && isset( $list_fields[ $attr['name'] ] ) ){
    		$html = str_replace('value="'. $attr['value'] .'"', 'value="'. $list_fields[ $attr['name'] ] .'"', $html );
    	}
    	return $html;
    },10, 2 );

    Kindly take note that you should replace the input field ID in the above-provided code (text-1) with the appropriate field ID from your specific form. You can add the code using a mu-plugin. To assist you in this process, I believe the following guide will be helpful: https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    Before implementing the code on your live website, it is highly recommended to test it thoroughly within a staging or development environment.

    Please feel free to get back to us if you need any clarification, we are happy to assist.

    Kind Regards,
    Nebu John