I am trying to auto fill a form (currently using contact form 7) with wordpress user details.
this is the current code:
<script type="text/javascript">
<?php
if (is_user_logged_in()){
global $current_user;
get_currentuserinfo(); ?>
document.getElementByName("your-name").value = "<?php $current_user->user_email ?>";
<?php
} ?>
</script>
I am using the contact form 7 plugin so it is called via short code.
Nothing seems to happen with this code but I have gotten it to echo me the user email so I don’t think that its a problem with fetching the data. If I had to bet, it would be a problem with filling the form with the data.
I have seen some auto fill plugins for contact form 7 but I also want to do this with other forms (specifically gravity forms) so I need a universal solution.
Any help is greatly appreciated, thanks.
First ensure that your contact form has HTML like
<input name='your-name' ...>Then try and put your code inside a window.onload event. Your javascript is probably running before the page has finished loading everything including your contact form.Something like this:
Edit: Fixed as per comments below