Skip Navigation

[Gelöst] Dynamically populate a Gravity Form field with a custom Types field

This support ticket is created vor 10 Jahre, 8 Monate. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 19:00 10:00 – 19:00 10:00 – 19:00 10:00 – 19:00 10:00 – 19:00 -
- - - - - - -

Supporter timezone: Europe/Madrid (GMT+01:00)

This topic contains 5 Antworten, has 2 Stimmen.

Last updated by Brent vor 10 Jahre, 7 Monate.

Assisted by: Caridad.

Author
Artikel
#79340

Hi, I'm trying to dynamically populate a gravity form with a custom field created with the Types plugin.
I figured creating a hook to do this would be the best way.
I have been able to do this with a custom user profile field like this, works great because it is associated with current logged in user:

add_filter('gform_field_value_address_line_1', create_function("", '$value = populate_usermeta(\'address_line_1\'); return $value;' ));

source: hidden link

This works fine for user profiles but I need to map to a custom post type and pull a custom field value from there based on the current logged in user if possible.

Does any one know of any GF hooks that will do this or something similar?

#79612

Dear Brent,

The hook to use would be the same one, but with different code. Lets say you have a "login" custom field to map your post type and you need the "address_line_1" custom field:

add_filter('gform_field_value_address_line_1', 'populate_address_line_1');
function populate_address_line_1() {
  global $current_user;
  $posts = get_posts('meta_key=wpcf-login&meta_value=' . $current_user->user_login);
  return get_post_meta($posts[0]->ID, 'wpcf-address_line_1', true);
}

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.

Regards,
Caridad

#79706

Trying to understand a little better about this part here,
$posts = get_posts('meta_key=wpcf-login&meta_value=' . $current_user->user_login);

This part, where you have set the meta value equal to the current users login:
&meta_value=' . $current_user->user_login);

Say I have a custom post type Winery.
I set up a custom field called Winery Name.
How would I write this?

add_filter('gform_field_value_winery_name', 'populate_winery_name');
function populate_winery_name() {
global $current_user;
$posts = get_posts('meta_key=wpcf-winery-name&meta_value=' . $current_user->user_login); //not sure about the end part here
return get_post_meta($posts[0]->ID, 'wpcf-winery-name', true);
}

#79778

add_filter('gform_field_value_address_line_1', 'populate_address_line_1');
function populate_address_line_1() {
global $current_user;
$posts = get_posts('meta_key=wpcf-login&meta_value=' . $current_user->user_login);
return get_post_meta($posts[0]->ID, 'wpcf-address_line_1', true);
}

So in your example would I need to create a custom login field for this custom post type and assign it the login value when the user first submits the form? -> wpcf-login

$posts = get_posts('meta_key=wpcf-login&meta_value=' . $current_user->user_login);

Here we grab the value of $posts per logged in user, yes?
return get_post_meta($posts[0]->ID, 'wpcf-address_line_1', true);

The last part : wpcf-address_line_1
What does this reference, the gravity form parameter or the custom field from the custom post type?

#79943

Dear Brent,

Yes, thats right. The login field is used to map the current user to a series of posts.
The last line, takes the address line field from the first post that we found for that user and its used to fill in the gravity forms input field.

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.

Regards,
Caridad

#86415

Thanks I got this working now.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.