admin管理员组

文章数量:1313006

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:

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:

.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
    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

    • [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 /
Keep the / 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

.646601418=xxxxxxx&entry.280532864=xxxxxxx&entry.1176284616=xxxxxxx&entry.165661554=xxxxxxx

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/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/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/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

    • [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/forms/d/e/
Keep the https://docs.google/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/forms/d/e/form-loooongID/viewform?entry.646601418=xxxxxxx&entry.280532864=xxxxxxx&entry.1176284616=xxxxxxx&entry.165661554=xxxxxxx
Share Improve this question edited Dec 17, 2016 at 3:29 marikamitsos asked Dec 16, 2016 at 18:38 marikamitsosmarikamitsos 5395 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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/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/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.

本文标签: javascriptHow to prefill Google Forms URLs with loggedin WordPress user39s data