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.

Pre-populate fields with (logged in) user info (eg. name, email)

  1. Hi guys, I'm a big fan of yours but a rare forum visitor, hoping you can help.

    I've created a form that can be seen by logged in users only, could I pre-populate some of the form fields (I'm specifically thinking of name/email for example) with the user's (already registered) data in the WordPress user database.

    That way they could tweak the info easily if they wanted or (more likely) leave it the same for the form submission, saving them typing time.

    Thanks,

    Nick

    Posted 12 years ago on Wednesday February 29, 2012 | Permalink
  2. Nick,

    You can do this by setting a default value for the field.

    Go to the field you want to populate automatically and click the edit link to expand the settings. Then click the 'Advanced' tab. About the 3rd option down you'll see 'Default value' and a drop-down list. At the bottom of that list are the items you are talking about, User Display Name and User Email. Just set the value you need and repeat for the other fields and you're good to go.

    Have a good one,
    -GB

    Posted 12 years ago on Wednesday February 29, 2012 | Permalink
  3. Thank you very much, that is a big help.

    It led me onto another thought though (and possibly stupid question). Is it possible to display that info in a field but 'lock' it so it can't be changed/edited, if you know what I mean?

    Thanks

    Nick

    Posted 12 years ago on Thursday March 1, 2012 | Permalink
  4. Sorry for the delay in reply Nick, I haven't checked back on the site until now.

    To make it 'locked' you'll need to set an attribute to read only which looks like this:

    <form>
      Name: <input type="text" name="userName" value="John Smith"
      readonly="readonly" /><br />
      <input type="submit" value="Submit" />
    </form>

    You could probably write a function to add the readonly="readonly" bit via jQuery to the field for each value you want to do that to. I haven't been able to test that or anything. I guess my only concern would be when the default value is set in relation with when the readonly attribute was set. If it was set to early the name couldn't be set.

    Best of luck!

    Posted 12 years ago on Tuesday March 6, 2012 | Permalink