我在我的WordPress网站上嵌入了一个freshdesk小部件。
当用户登录WordPress网站时,我需要使用WordPress的用户凭据预填充电子邮件字段。到目前为止,我自己或支持都没有运气。有人知道WordPress如何存储用户电子邮件以及如何从WordPress获取它?
这是我目前的代码:
<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>
答案 0 :(得分:1)
首先获取用户ID此功能内置于wordpress中以用于其他用途
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 );
}
然后在您自己的模板页面中调用函数
<?php $user_info = get_userdata(get_current_user_id());
$email = $user_info->user_email;
?>
你可以使用php jquery的组合
$input = $("your input selector");
$input.attr("value","<?php echo $email; ?>");
最后找到要预填充的字段,并使用$ email填充value属性。我建议使用jQuery,而不是修改插件,因为更新可能会消除您所做的任何更改。
答案 1 :(得分:1)
如果已登录,请先获取当前用户的电子邮件地址。
<?php
$user_id = get_current_user_id();
$email = '';
if ($user_id > 0) {
$user_info = get_userdata($user_id);
$email = $user_info->user_email;
}
?>
然后,您可以嵌入Freshdesk反馈小部件代码以包含上述电子邮件。
<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>