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.
Why?
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.
How?
This is the actual link for the demo form for this post:
https://docs.google.com/forms/d/e/1FAIpQLSeChqhovV70FbILIlfeo8K5UL9q3hfjPkngEe4IJkXMmCbawg/viewform
We always keep the same pattern (as far as structure goes) for the first sections that identify the student.
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
https://docs.google.com/forms/d/e/
2nd part of the link: the ID of the specific form
1FAIpQLSeChqhovV70FbILIlfeo8K5UL9q3hfjPkngEe4IJkXMmCbawg
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
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
https://docs.google.com/forms/d/e/form-loooongID/viewform?entry.646601418=xxxxxxx&entry.280532864=xxxxxxx&entry.1176284616=xxxxxxx&entry.165661554=xxxxxxx