PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

right parameter names to pre-populate the names fields?

  1. 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?

    Posted 13 years ago on Sunday January 16, 2011 | Permalink
  2. 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.

    Posted 13 years ago on Sunday January 16, 2011 | Permalink
  3. 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.

    Posted 13 years ago on Monday January 17, 2011 | Permalink
  4. 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

    1. Paste the following code into your functions.php:
      (old method) http://pastie.org/1470060
      (updated for WP 3.3) http://pastie.org/3174147
    2. Specify your dynamic population parameters on your name field like so: http://grab.by/8qjd
    3. Done!

    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:

    1. Add a new dynamic population parameter to the field you want to populate.
    2. Add a new filter in your functions.php like this:
      add_filter('gform_field_value_user_lastname', create_function("", '$value = populate_usermeta(\'last_name\'); return $value;' ));
    3. And lastly, update that line to reflect your new population parameter and the user meta property you want to populate the field with. In the example below, I want to get the user description.
      add_filter('gform_field_value_user_description', create_function("", '$value = populate_usermeta(\'description\'); return $value;' ));
    4. Hope that makes sense!

    Posted 13 years ago on Monday January 17, 2011 | Permalink
  5. Thanks a lot David,
    Just perfect, theese functions really rocks ... thanks for the list thats all I need for now:-)

    Posted 13 years ago on Friday January 21, 2011 | Permalink
  6. azazure
    Member

    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!

    Posted 13 years ago on Wednesday February 9, 2011 | Permalink
  7. 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.

    Posted 13 years ago on Wednesday February 9, 2011 | Permalink
  8. @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

    Posted 13 years ago on Wednesday February 9, 2011 | Permalink
  9. azazure
    Member

    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.

    Posted 13 years ago on Thursday February 10, 2011 | Permalink
  10. azazure
    Member

    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 '';
    }

    ?>'

    Posted 13 years ago on Thursday February 10, 2011 | Permalink
  11. 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.

    Posted 13 years ago on Thursday February 10, 2011 | Permalink
  12. azazure
    Member

    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.

    Posted 13 years ago on Thursday February 10, 2011 | Permalink
  13. azazure
    Member

    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.

    Posted 13 years ago on Saturday February 19, 2011 | Permalink
  14. phirefly
    Member

    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 '';
    }
    Posted 13 years ago on Thursday April 7, 2011 | Permalink
  15. 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?

    Posted 13 years ago on Thursday April 7, 2011 | Permalink
  16. phirefly
    Member

    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!

    Posted 13 years ago on Friday April 8, 2011 | Permalink
  17. 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:

    http://pastie.org/3174155

    Posted 12 years ago on Thursday January 12, 2012 | Permalink

This topic has been resolved and has been closed to new replies.