Reputation: 25
First off I just wanted to say that this is my first question on StackOverflow, so I want to apologize now since I will probably make some mistakes writing this.
So I got a wordpress website with user login feature and I'm offering support through the Zendesk Web Widget which basically is a support contact form. I'm trying to pre-populate the Display Name & Email fields in the support form with the current logged-in user information.
Throught my research, I found that Wordpress already has a function for this : get_currentuserinfo(), and that I could pass the data to javascript by adding this to my functions.php child theme :
function get_user($type = 'ID')
{
global $current_user;
get_currentuserinfo();
switch ($type)
{
case 'ID':
return $current_user->ID;
break;
case 'displayname':
return $current_user->display_name;
break;
case 'username':
return $current_user->user_login;
break;
case 'firstname':
return $current_user->user_firstname;
break;
case 'lastname':
return $current_user->user_lastname;
break;
case 'level':
return $current_user->user_level;
break;
case 'email':
return $current_user->user_email;
break;
default:
return;
}
}
And by using echo get_user('displayname');
to use the display name in javascript.
Source : http://www.devdevote.com/cms/wordpress-hacks/get_user
So I made some attempts with it, but none worked out. I tried these :
Attempt 1 :
<script>
var $wpName = <?php echo get_user('displayname') ?>;
var $wpEmail = <?php echo get_user('email') ?>;
zE(function() {
zE.identify({
name: $wpName,
email: $wpEmail
});
});
</script>
Attempt 2 :
<script>
zE(function() {
zE.identify({
name: <?php echo get_user('displayname') ?>,
email: <?php echo get_user('email') ?>
});
});
</script>
Attempt 3 :
<script>
var $wpName = $current_user->user_firstname;
var $wpEmail = $current_user->user_email;
zE(function() {
zE.identify({
name: <?php echo get_user('displayname') ?>,
email: <?php echo get_user('email') ?>
});
});
</script>
Does anyone out here have an idea or just a clue on how to to do this ? I've been struggling all day trying to make this work, reading about 20 to 30 threads on the subject, but none had a working solution for me..
P.S. Once again I'm sorry if I made a mistake posting here or if I didn't write out my question well, this is my first time !
Upvotes: 0
Views: 2360
Reputation: 25
Turns out I figured out a better way to do this, thanks to Erik Westlund here on StackOverflow
It wasn't working because PHP inside my javascript code wasn't processed, so any echo statement would result in a failure.
So I had to turn myself to another solution : json_encode()
So with just these few line of codes added to a new page template file, I was able to make it work :
<?php global $current_user;
get_currentuserinfo();
$user_name = $current_user->display_name;
$user_email = $current_user->user_email;
?>
<script type="text/javascript">
var username = <?php echo json_encode($user_name) ?> ;
var useremail = <?php echo json_encode($user_email) ?> ;
</script>
Then I just had to use it in my javascript like this :
zE(function() {
zE.identify({
name: username,
email: useremail
});
});
Hopefully this can be useful for somebody in the future !
Upvotes: 1