UsersWP Default Value for Gravity Forms

This topic contains 11 replies, has 3 voices, and was last updated by  Stiofan O’Connor 6 years, 4 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
  • Author
    Posts
  • #1021

    Joey Nizuk
    Free User
    Post count: 7

    Hello,

    I’m trying to setup default values for gravity form input fields using the registered user data.

    To pull the default wordpress user value in gravity forms I would use {user:first_name}, but how to I pull the UsersWP user values?

    #1026

    Stiofan O’Connor
    Site Admin
    Post count: 22956

    Hello,

    The first/last name should be exactly the same, we use the WP values for that.
    Other non standard values i don’t think can be added yet, if Gravity forms has a filter for that we might be able to add our parameters. I’ll try and contact them to see if they can provide us a developer licence to add integrations.

    Thanks,

    Stiofan

    #1040

    Joey Nizuk
    Free User
    Post count: 7

    Hey Stiofan,

    Thanks for the reply. I did some more digging on my end and I also reached out to Gravity Forms to see what they would recommend and this is what they told me:

    If you need to pull from another database table, you won’t be able to use the Gravity Forms usermeta merge tags. You can use this filter to pull data from anywhere to populate fields in your form (it’s not specific to UserWP):

    https://docs.gravityforms.com/gform_field_value_parameter_name/

    So went ahead and tried to set that up, but I don’t know how to call the UserWP database tables for the uwp_usermeta.

    This is what I’ve tried so far: got conformation from Gravity forms that you can not use merge tags or the default values unless its for wordpress or gravity forms user data, but you can use parameters. So using the help article Gravity forms sent me I setup my parameters for my data (see screen shot) and added the following filter which returns a value from the database. This isn’t working and I’m not sure how I would go about pulling the upw_usermeta data from the database of current logged in user.

    
    
    // define the fields we'll be populating
    $fields = array('uwp_account_company', 'uwp_account_phone');
     
    // loop through fields and add the Gravity Forms filters
    foreach($fields as $field)
      add_filter('gform_field_value_'.$field, 'my_populate_field');
     
    
    // the callback that gets called to populate each field
    function my_populate_field($value){
      // we have to wrestle the field name out of the filter name,
      // since GF doesn't pass it to us
      $filter = current_filter();
      if(!$filter) return '';
      $field = str_replace('gform_field_value_', '', $filter);
      if(!$field) return '';
     
      // get the current logged in user object
      $user = wp_get_current_user();
     
      // We'll just return the user_meta value for the key we're given.
      // In most cases, we'd want to do some checks and/or apply some special
      // case logic before returning.
      return get_user_meta($user->ID, $field, true);
    }

    What are your thoughts?

    #1044

    Stiofan O’Connor
    Site Admin
    Post count: 22956

    if you can give me wp-admin details in a private reply(only staff can see) i’ll run some tests on your site and if i get it working i’ll likely add it to core’s next release.

    Thanks,

    Stiofan

    #1089

    Joey Nizuk
    Free User
    Post count: 7
    This reply has been marked as private.
    #1090

    Alex Rollin
    Moderator
    Post count: 27815
    This reply has been marked as private.
    #1091

    Joey Nizuk
    Free User
    Post count: 7
    This reply has been marked as private.
    #1102

    Stiofan O’Connor
    Site Admin
    Post count: 22956

    Can you please check your FTP details, non of the team can connect with the above details.

    Thanks,

    Stiofan

    #1105

    Joey Nizuk
    Free User
    Post count: 7
    This reply has been marked as private.
    #1108

    Alex Rollin
    Moderator
    Post count: 27815

    Hello,

    I can confirm the credentials worked for me.

    #1109

    Joey Nizuk
    Free User
    Post count: 7

    cool thanks guys!

    #1120

    Stiofan O’Connor
    Site Admin
    Post count: 22956

    I have added the following snippet but not tested it, please test it and let us know.
    For example if you have a custom field with html_var of “gender” then you would add a GF field of “uwp_account_gender” and that should work.

    Please let us know how u get on.

    Stiofan

    
    
    add_filter( 'gform_field_value', '_gf_populate_uwp_fields', 10, 3 );
    function _gf_populate_uwp_fields( $value, $field, $name ) {
     
         $user_id = get_current_user_id();
      if(!$user_id){return $value;}
      
      $values = array();
      
    	 $usermeta = uwp_get_usermeta_row($user_id); 
    	  if(!empty($usermeta)){
    		foreach($usermeta as $key=>$meta){
    		  if (strpos($key, 'uwp_account_') === 0) {
    				$values[$key] = $meta;
    		  }
    		}
    	  }
    
     
        return isset( $values[ $name ] ) ? $values[ $name ] : $value;
    }
Viewing 12 posts - 1 through 12 (of 12 total)

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket