Quantcast
Channel: WordPress.org Forums » All Topics
Viewing all articles
Browse latest Browse all 59525

How To Get Current User’s Name, Email + User Role via API

$
0
0

Replies: 0

After 2 days try and error I’ve finally figured out how to get the name, email and user role from wordpress logged in users into the Tidio Chat…

Just paste this code into your template’s footer.php before

</body>
</html>

and it will automatically update the user’s name, email and role from WordPress, even if the user’s role changes.

This is very useful if you have different WordPress membership roles and want to know if for example just a “Guest”, a “Silver” or “Gold” member is writing you.

<?php if ( is_user_logged_in() ) { ?>
<script>
tidioChatApi.setVisitorData({
email: "<?php $current_user = wp_get_current_user();
echo $current_user->user_email; ?>",
name: "<?php $current_user = wp_get_current_user();
echo $current_user->display_name; ?>",
tags: ["<?php
global $wp_roles;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
echo $wp_roles->roles[ $user_role ]['name']; ?>"]
});
</script>
<?php } else { ?>
<script>
tidioChatApi.setVisitorData({
tags: ["Guest"]
});
</script>
<?php } ?>

Holy cow, this was a nightmare to put this together. But now it works like a charm! I should charge for this.. xD


Viewing all articles
Browse latest Browse all 59525

Trending Articles