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.

Populating password field

  1. vlinkdave
    Member

    On my registration form I'd like the user to enter their email address and then use the address that is entered as the password value.

    Is their a filter available that will auto-generate the password using the email address?

    thx... dave

    Posted 12 years ago on Wednesday August 3, 2011 | Permalink
  2. Hi Dave. I was able to get this to work by using the gform_pre_submission filter. http://www.gravityhelp.com/documentation/page/Gform_pre_submission

    In my form, I have a required email address field, and an admin only single line of text that I am using as the password. You can't use the password field here for a couple reasons, and it's unnecessary anyway as we don't need any of the password-specific functions.

    My email address field is input_2 (where the value is coming from), and input_7 is the password field (where the value is going, i.e. email address is being copied over to the password field.)

    I added this to my theme's functions.php:

    // copy the email address value over to the admin only password field
    add_action('gform_pre_submission_4', 'ch_populate_password');
    function ch_populate_password($form){
            $_POST['input_7'] = rgpost('input_2');
    }

    For form ID is 4 at the end of the gform_pre_submission filter. Change the ID to match the ID of your form.

    So, after the form is submitted and submitted text validated, this function takes the value of "input_2" which is my email address field, and copies it over to "input_7" in the $_POST variable before the entry is stored and the notifications are mailed out.

    If you have any trouble implementing that, please let us know.

    Thanks.

    Posted 12 years ago on Thursday August 4, 2011 | Permalink
  3. vlinkdave
    Member

    Thanks for the help... I do appreciate it.

    Is there an easier way to learn what the field id values are other than looking at my page html source code?

    Since the password is being sent to the WP interface, will there be any difficulty mapping a text field instead of the normal password field as normally expected by the User Registration feed?

    Does the pre_submission hook actually display the email address in the password field when it is entered by the registrant or only after the submit button is pressed?

    Thanks again for the guidance... dave

    Posted 12 years ago on Thursday August 4, 2011 | Permalink
  4. For learning the field values, looking at the source is probably easiest. There is also a way to add the field ID to your admin screen, but I would not say it's easier. It's certainly more convenient. Least easy way is to dump the whole $_POST variable and look at all the field IDs. It's not easy, but the information is all there.

    With the password being mapped to this plain text field, I made a mistake. You can't map a plain text field to the password in the user registration feed. I am going to look for a way to take care of that. Sorry about my mistake there. I forgot about mapping the field and found out today it did not work.

    The pre_submission hook does NOT display the email address in the password field because the password field is marked admin only. So, when the form is displayed, there is no password field shown. After the form is submitted, the hook fills that form value in by setting it to the email that was submitted. Then, in your admin, you will see a form field of "Password" with the email address there.

    I am going to check for a way to set the password to the email address and have it mapped properly to the password in the registration feed now.

    Posted 12 years ago on Thursday August 4, 2011 | Permalink
  5. HI Dave, I think I have it sorted out and it's a little simpler too. I added this to my functions.php:

    add_action('gform_user_registered', 'ch_set_password_to_email', 10, 3);
    function ch_set_password_to_email($user_id, $config, $entry) {
    	wp_set_password($entry[2], $user_id);
    }

    That will update the password for the user that was just created to the value of $entry[2], which is input 2 in my form, which corresponds to the email address. The WordPress function wp_set_password takes care of the hashing and clearing the cache.

    In the form, you can remove the admin only password field altogether. In the registration feed, the Password will be mapped to "Auto Generate Password" because there is no password field present in the form. That's OK. Gravity Forms will auto-generate the password and insert that into the wp_users table. Then with the gform_user_registered hook you're going to change it over to the email address.

    In the registration feed settings, I unchecked the option "[  ] Send this password to the new user by email" since it would be the auto-generated password and would not work.

    Then, in my notifications, I created a user notification that looks similar to the WordPress one, with the username and password in there.

    You can test it out here:
    http://gravity.chrishajer.com/register/

    Let me know if you have any questions. Thank you for your patience.

    Posted 12 years ago on Thursday August 4, 2011 | Permalink