Reputation: 115
I've embedded a freshdesk widget on my WordPress website.
I need to pre-populate the email field with the user credentials from WordPress when the user is logged in to WordPress website. So far I had no luck on my own or with the support. Does somebody know how WordPress stores user email and how to get it from WordPress?
This is my current code:
<script src="http://assets.freshdesk.com/widget/freshwidget.js" type="text/javascript"></script>
<script type="text/javascript">
FreshWidget.init("", {"queryString": "&helpdesk_ticket[requester]={{user.email}}&helpdesk_ticket[subject]={{user.subject}}&helpdesk_ticket[custom_field][phone_number]={{user.phone}}&helpdesk_ticket[custom_field][product_id]={{helpdesk.product}}",
"widgetType": "popup", "buttonType": "text", "buttonText": "Support",
"buttonColor": "white", "buttonBg": "#338700", "alignment": "4",
"offset": "235px", "formHeight": "500px",
"url": "http://xxxxxx.freshdesk.com"} );
</script>
Upvotes: 1
Views: 685
Reputation: 120
Lets first get the email address of the current user, if logged in.
<?php
$user_id = get_current_user_id();
$email = '';
if ($user_id > 0) {
$user_info = get_userdata($user_id);
$email = $user_info->user_email;
}
?>
And then after this you can embed the Freshdesk feedback widget code to include the above email.
<script src="http://assets.freshdesk.com/widget/freshwidget.js" type="text/javascript"></script>
<script type="text/javascript">
FreshWidget.init("", {
"queryString": "&helpdesk_ticket[requester]=<?= urlencode($email) ?>",
"widgetType": "popup", "buttonType": "text",
"buttonText": "Support",
"buttonColor": "white", "buttonBg": "#338700",
"alignment": "4",
"offset": "235px", "formHeight": "500px",
"url": "http://xxxxxx.freshdesk.com"} );
</script>
Upvotes: 1
Reputation: 333
first get users id This function is built into wordpress for its other uses
function get_current_user_id() {
if ( ! function_exists( 'wp_get_current_user' ) )
return 0;
$user = wp_get_current_user();
return ( isset( $user->ID ) ? (int) $user->ID : 0 );
}
Then in your own template page call the function
<?php $user_info = get_userdata(get_current_user_id());
$email = $user_info->user_email;
?>
you can use a combination of php jquery
$input = $("your input selector");
$input.attr("value","<?php echo $email; ?>");
from Ref first, Ref Second
Finally find the field you want to pre-populate and fill the value attribute with $email. I would suggest jQuery, rather than modifying the plugin, since an update will likely wipe any changes you make.
Upvotes: 1