What's the right parameter names to pre-populate the names fields?
I want to fill in the username in a form, but I cannot get it right, I have tried.....
{embed_user:user_firstname}
{embed_user:user_lastname}
but maybee it aint that easy?
What's the right parameter names to pre-populate the names fields?
I want to fill in the username in a form, but I cannot get it right, I have tried.....
{embed_user:user_firstname}
{embed_user:user_lastname}
but maybee it aint that easy?
How are you trying to pre-populate them? There are 2 ways to prepopulate a field.
1) Via the Querystring (ex. domain.com/myform?firstname=Bob&lastname=Doe )
2) Via PHP and Gravity Forms hooks
I'm not sure what these are:
{embed_user:user_firstname}
These aren't valid form field variables.
If you explain what you are trying to do, what you are trying to pre-populate the name field with (the user that is logged in?) we can provide you with details on how to do what you are trying to do.
I want to pre-populate them via PHP and Gravity Forms hooks with the user that is logged in. I have been able to pre populate the e-mail field.... but the name fields looks a bit different.
This is probably a bit overkill... but here is a solution that will let you retrieve any piece of user meta without having to create tons of bulky functions.
Steps to Install
Now, if you'd like to get any other piece of user meta, all you need to know is the property it is stored in within the $current_user object. There are a lot, but here is a list of the common ones:
first_name
last_name
user_login
user_pass
user_nicename
user_email
user_url
user_registered
user_status
display_name
nickname
description
aim
yim
jabber
So to get any of these properties to auto-populate a field, just:
add_filter('gform_field_value_user_lastname', create_function("", '$value = populate_usermeta(\'last_name\'); return $value;' ));
add_filter('gform_field_value_user_description', create_function("", '$value = populate_usermeta(\'description\'); return $value;' ));
Hope that makes sense!
Thanks a lot David,
Just perfect, theese functions really rocks ... thanks for the list thats all I need for now:-)
I am new here so please bear with me. I modified my wp-includes\functions.php file as indicated above but received the following error when refreshing my website:
Fatal error: Call to undefined function add_filter() in /home/content/81/7305981/html/wp-includes/functions.php on line 4
I am using WP 3.04
Thanks in advance!
We would have to see the code you are implementing, you can post it here and if you wrap it in backtick characters which is this ` then it will format the code for you so it is readable okay here.
@azazure, you were not supposed to modify wp-includes/functions.php. You were supposed to add that code to the functions.php file in your active theme. Please replace wp-includes/functions.php with a stock copy and then make the modification to the functions.php in your theme folder. I think it will work for you then.
If you cannot undo the edits you made, you can always get a fresh copy of the file here:
http://svn.automattic.com/wordpress/tags/3.0.4/wp-includes/functions.php
Thank you, I was not aware that there was more than one functions.php. I made a backup copy of the file before I edited it so when it quit working I quickly replaced it with the original! Thank you for your response, it is greatly appreciated. I will try it again using the correct file.
OK, I must be stupid. I am editing the correct functions.php file now in my atahualpa theme and have set the form fields to populate but nothing happens. I have read the instructions and edited the form a hundred times to make sure I'm doing it correctly, but I just can't get it to work. I even tried a different field (email) to see if it would work, but no dice. Here is the code I'm putting at the top of my functions.php file:
'<?php
// populate the field with "user_firstname" as the population parameter with the "first_name" of the current user
add_filter('gform_field_value_user_firstname', create_function("", '$value = populate_usermeta(\'first_name\'); return $value;' ));
// populate the field with "user_lastname" as the population parameter with the "last_name" of the current user
add_filter('gform_field_value_user_lastname', create_function("", '$value = populate_usermeta(\'last_name\'); return $value;' ));
// populate the field with "user_email" as the population parameter with the "user_email" of the current user
add_filter('gform_field_value_user_email', create_function("", '$value = populate_usermeta(\'user_email\'); return $value;' ));
// this function is called by both filters and returns the requested user meta of the current user
function populate_usermeta($meta_key){
global $current_user;
get_currentuserinfo();
foreach($current_user as $key => $value){
if($key == $meta_key)
return $value;
}
return '';
}
?>'
Contact us via our Contact Us form and provide us with a WordPress admin login and FTP login for this site and a detailed description of what you are trying to do and we can look into this, check out your code and see what you are doing wrong.
I'm also using Register Plus Redux if that makes a difference. It's a free plugin which allows you to add additional fields to the registration. BTW, I LOVE this plugin. It is making short work out of what I thought would be an impossible job.
I FINALLY figured out why I was having problems with the above code for the theme functions.php file. Using "//" before the comments was not allowing the code to be read for some reason. Since I like to document everything, I found that using /* and */ on the comments allowed everything to work perfectly.
Hello,
I too am using this method (I also use Register Plus Redux plugin) & it seems to work...nearly!
Additional fields (order_id / phone) created using Register Plus Redux are working fine - Gravity forms are dynamically populating the fields, but the "normal" Wordpress ones are not (first_name / last_name / user_url / user_email).
Would appreciate any assistance!
/* populate the field with "user_firstname" as the population parameter with the "first_name" of the current user */
add_filter('gform_field_value_user_firstname', create_function("", '$value = populate_usermeta(\'first_name\'); return $value;' ));
/* populate the field with "user_lastname" as the population parameter with the "last_name" of the current user */
add_filter('gform_field_value_user_lastname', create_function("", '$value = populate_usermeta(\'last_name\'); return $value;' ));
/* populate the field with "phone" as the population parameter with the "phone" of the current user */
add_filter('gform_field_value_phone', create_function("", '$value = populate_usermeta(\'phone\'); return $value;' ));
/* populate the field with "order_id" as the population parameter with the "order_id" of the current user */
add_filter('gform_field_value_order_id', create_function("", '$value = populate_usermeta(\'order_id\'); return $value;' ));
/* populate the field with "email" as the population parameter with the "user_email" of the current user */
add_filter('gform_field_value_email', create_function("", '$value = populate_usermeta(\'user_email\'); return $value;' ));
/* populate the field with "business_website" as the population parameter with the "user_url" of the current user */
add_filter('gform_field_value_business_website', create_function("", '$value = populate_usermeta(\'user_url\'); return $value;' ));
/* this function is called by both filters and returns the requested user meta of the current user */
function populate_usermeta($meta_key){
global $current_user;
get_currentuserinfo();
foreach($current_user as $key => $value){
if($key == $meta_key)
return $value;
}
return '';
}
Just tested on my localhost and it was working fine. Can you confirm that you're using "user_firstname" and "user_lastname" as the dynamic population parameters on the fields?
Hi David,
many thanks - I am a muppet! Reading your post & referring back to my code it all makes sense now (I was using "first_name")
Many Thanks!
For users using this method to populate user meta, you will need to update the populate_meta() function from the original snippet with this updated version for WP 3.3: