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?
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
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);
}
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?
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
Thanks I got this working now.