0

I want to create my own form in order to get information on my site. For learning purposes, I do not want to use a plugin. So, please don't tell me to use one. I know they are easy to use, but I want to get under the hood in WordPress.

I would like to populate my form with email and name if the user is logged in, but I am a bit at a loss on how to do this. Maybe a javascript snippet or a bit of PHP?

I don't know which is better or the standard now. It's been a while and I really want to try doing this on my own.

Please any Gurus can you help?

1
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.
    – Community Bot
    Sep 13 at 15:28
0

I am assuming you are using third party forms, if yes Then you have to use Javascript and PHP. You can paste this code in shortcode or you have to use hooks, as you are learning please go through this Link

PHP CODE

if ( is_user_logged_in() ) 
{        
  $current_user = wp_get_current_user();
  $email = $current_user->user_email
}

See the documentation for wp_get_current_user()

Now as you have $email you can save that $email to JavaScript something like this

<script>var email = <?PHP echo $email ?></script>

Now using this var you can populate these user details to the form.

If you face any issue let me know.

4
  • I don't understand why you are recommending to read about wp_footer.
    – fuxia
    Sep 10 at 16:24
  • I created my own form from scratch. I'm trying to figure out how to add a script into populate the email and username if they are logged in. I think I get what you mean, though. I was wondering what words to use like wp_user or something like that.
    – Mac
    Sep 11 at 0:17
  • @fuxia Yes your are right, the reason I recommend him, as he mentioned that he is doing this for learning purpose so may be he is new to WP so to make thing simple i recommend him to go with that wp_footer so that he can insert his own script. Sep 11 at 8:38
  • @Mac wp_get_current_user(); is a global function to get object of wp_user. You can try my solution, it will work. Sep 11 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.