We need to have our logged-in users' data (username , Email , First Name , Last Name) pre-fill the links to Google Forms existing on our site.


We have Google Forms spread out on our site that our students (youngsters aged 6-11) under the customized role students, take as tests. The problem is that they keep making mistakes when writing their names, emails, etc.
The links to the forms look like: Click here for the test

So, we would like to pre-fill these various links with their User Data as they are stored in the WordPress database.


This is the actual link for the demo form for this post:

We always keep the same pattern (as far as structure goes) for the first sections that identify the student.

user data

If we use the pre-fill feature of the form for:

LoginName: STUDENT 01  
Email: [email protected]
FirstName: First
LastName: Last

We get the following link:

https://docs.google.com/forms/d/e/1FAIpQLSeChqhovV70FbILIlfeo8K5UL9q3hfjPkngEe4IJkXMmCbawg/viewform?entry.646601418=STUDENT+01&[email protected]&entry.1176284616=First&entry.165661554=Last

Breaking down and analyzing the link, we notice the following parts:

  • 1st part of the link: telling Google it is a form

  • 2nd part of the link: the ID of the specific form

  • 3rd part of the link /viewform/ followed by a ?

  • 4th part of the link: this is where exist the positions and the data of the pre-fill.
    Since we always keep -as mentioned- the same structure, we will always get the same positions joint by &, like:

    • entry.646601418=STUDENT+01

    • [email protected]

    • entry.1176284616=First

    • entry.165661554=Last

How to solve

I think that using an if action that combines current_user_can( 'student' ) , public function init , wp_get_current_user() and maybe some JS with onclick could solve this issue by replacing the above mentioned data with the ones retrieved from WordPress for a logged-in student for every link starting with the 1st part that identifies a Google Form.

Unfortunately I am lost on how to do this. So.
How could one pre-fill Google Forms URLs with logged in WordPress user's data?
Or in other words:
How to dynamically populate already existing external links with user data in WordPress.

EDIT: Further clarification

There is not just one Google Form present, but many. This means that the 2nd part of the URL (Form ID ) is constantly changing.

So the workflow to the solution could be an action where:

If user has the role student (or is logged)
And if any given URL starts with https://docs.google.com/forms/d/e/
Keep the https://docs.google.com/forms/d/e/ part
Keep the part of the looooong Form ID until the next /
Add a /viewform/?
Add entry.646601418= And (dynamically) Fill in current Login Name
Add entry.280532864= And (dynamically) Fill in current Email
Add entry.1176284616= And (dynamically) Fill in current First Name
Add entry.165661554= And (dynamically) Fill in current Last Name
Produce" onclick for non-admin pages the new URL


1 Answer 1


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.

  • 1
    Thank you Gareth for the quick reply. Still. Since my head is ready to explode trying to combine the above, would you be so kind as to provide some full working code (something in the lines of this post). This will help others as well to understand how your code works. Couple of points to remember: Needs to be applied every time but only after one clicks on the link, of various (already existing and future ones) form IDs for every logged-in student. Dec 16, 2016 at 19:01
  • 1
    Bonus points if when a user is not logged in the site, it is just the email that defaults to a custom one ([email protected]) Dec 16, 2016 at 19:07
  • 1
    give me an hour and I will :) Dec 16, 2016 at 19:33
  • 1
    Great. I can now go back correcting papers. :( As I said, it needs to be a "smart" solution so whenever a link is found (existing or future) that starts with the 1st part, after the second part (the form ID which should be left intact but will be different every time, including the /viewform/) all the rest like email, First Name, etc should be populated by WordPress. Thanks again :) Dec 16, 2016 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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