Search code examples
phpwordpressauthenticationuser-inputgravityforms

WordPress User Registration with Custom Fields


I am currently working on a WordPress website in which it is optional for a user to create an account and save their information via Gravity Forms. (Address, billing address, phone number, email, and name.) This information is then supposed to be used to "dynamically populate" any forms, where that information is required, that the user wishes to submit. WordPress and Gravity Forms allow some of the user data to be dynamically populated, but not anything from a custom field in the registration form. In my case, address, billing address, and phone number. So how do I make user data submitted in one Gravity Form's custom field populate another Gravity Form with the same field?


Solution

  • There may be a plugin or two that could help you, but I can't attest to that. The native way to dynamically populate form fields won't work with what you're describing. If you look through the Using Dynamic Population documentation, your best option appears to be the Hooks section.

    For example, I'll modify the example code a bit:

    add_filter( 'gform_field_value_users_favorite_color', 'gform_dynamic_users_favorite_color' );
    function gform_dynamic_users_favorite_color( $value ){
        if( $user_id = get_current_user_id() ){
            if( $favorite_color = get_user_meta($user_id, 'users_favorite_color', true ) ){
                return $favorite_color;
            }
        }
    }
    

    If you use the above filter/function, it will populate any field that has the dynamic population parameter name users_favorite_color and populate it with the current users's favorite color, if it exists. You can add a filter for gform_field_value_ANY_PARAMETER_YOU_WANT, attach a function to it, and get it return whatever values you wish this way.