How to pre-fill Google Forms URLs with logged-in WordPress user’s data

AS easy as you said:

$user = wp_get_current_user();
if(!empty($user)) {
 $email = $user->user_email;
 $fname = $user->user_firstname;
 $sname = $user->user_lastname;
 $url="https://docs.google.com/forms/d/e/1FAIpQLSeChqhovV70FbILIlfeo8K5UL9q3hfjPkngEe4IJkXMmCbawg/viewform?entry.646601418=STUDENT+01&entry.280532864=".$email.'&entry.1176284616='.$fname.'&entry.165661554='.$sname;
 echo $url;
} else {
 $email="[email protected]";
 $url="https://docs.google.com/forms/d/e/1FAIpQLSeChqhovV70FbILIlfeo8K5UL9q3hfjPkngEe4IJkXMmCbawg/viewform?entry.646601418=STUDENT+01&entry.280532864=".$email.'&entry.1176284616=First&entry.165661554=Last';
 echo $url;
}

If the user is logged in (if !empty ($user) ) then it grabs the data from the user variable wp_get_current_user, we could also wrap in is_user_logged_in but shouldn’t be necessary for this as we are after the user data.

I am not sure why you need Javascript, the url is now populated so when that person sees the form they should see the form filled in with their data.

You will need to delete and replace all the existing URL’s on the site to use the new “version” but that’s a little beyond my remit as you would need to do 3 things:

1) with a function, hook into the_content (or the_excerpt) and find instances of the url.
2) add my code to get the user details
3) preg replace the URL with the new one.

My code should get you most of the way except for the old url’s but if you ask a different question on how to preg_replace a url using the_content filter, someone should be able to help you out.

Leave a Comment