Table of Contents

How to pass WordPress logged in user data to Landbot

Abby Updated by Abby

Being able to identify logged in users can be very important for some businesses and websites.

We can achieve that by passing the lead's information in an identity in the embed code. We are using WordPress here as an example, but the same logic should work for any kind of site.

Type of embed

In order to use this example we need to embed our bot using the File Manager plugin, following the steps outlined in this article. The article mentions full-page, but you can use any embed type you want.

Preparing the code

The first step is to customize the code responsible for passing the identity, we will pass the emailfirst namelast nameuser name, and a static custom field called "logged_in" we will use it to build the bot's logic.

We will use a wordpress function to check IF the user is logged, we will pass the data, ELSE we will display the normal embed code without any data in it.

<?php
/**
* Template Name: LANDBOT
* Template Post Type: page
*
*/
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
echo '<script>var landbotSettings = {email: "'. $current_user->user_email . '", name: "' . $current_user->user_firstname . ' ' . $current_user->user_lastname .'", wp_user_name: "' . $current_user->user_login . '", wp_logged_in: "Yes" };</script>';}
else {
echo '<script>var landbotSettings = { wp_logged_in: "no" };</script>';}
?>

<script SameSite="None; Secure" src="https://cdn.landbot.io/landbot-3/landbot-3.0.0.js"></script>
<script>
var myLandbot = new Landbot.Livechat({
configUrl: 'https://chats.landbot.io/v3/H-944207-1QLFV9X1D34Q7VMP/index.json',
customData: {wp_info : landbotSettings}
});
</script>

Let's explain the code:

We'll started by adding the template name that will later tell us where to find the bot, then in the same PHP tags we created an 'IF/ELSE' statement, this statement checks to see if the user is logged in, if that's true it will return all of the available user account details we have available, if it's not true it will go to the 'Else' statement.

We will output the static part of the embed code between single quotes ' ', and when there is info we want to pass we will use one of the following default wordpress functions after we close the single quote, then we will start it again to continue the static part, like it is shown in the above example.

  • Username-> . $current_user->user_login .
  • User email-> . $current_user->user_email .
  • User first name-> . $current_user->user_firstname .
  • User last name-> . $current_user->user_lastname .
  • User display name-> . $current_user->display_name .
  • User ID-> . $current_user->ID .

Notice how we passed two custom fields  wp_user_name  which is an info we need to get from WP, that's why we used the  . $current_user->user_login . function. While the custom field wp_logged_in is a static one that we need to fill with "Yes" to run a conditional logic against it. In most cases, the above code is more than enough, you will get the important info to identify your logged in users, so you can just copy the code as it is unless you want to add more custom fields or remove some custom fields.

Where to insert the code

This code has PHP functions, it needs to be executed in the server side.

How to build your bot to differentiate between logged-in users

For this example, we will create a new bot from scratch, then add a conditional logic to check if the user is logged in or not.

Using the 'GetValue' method on the object we will be able to get any information we want using the key.

We were able to retrieve the 'Is logged in' information using a formula (we no longer need to add ToJSON):

GetValue(@wp_info, 'wp_logged_in')

We then used a condition to see if it's true and divided the flow based on the response:

Now let's test it!

How to identify logged in users for other CMS and websites

As long as you apply the same logic, you will be able to pass data to Continually from any Content Management System or custom websites, you need to know three things :

  1. The IF ELSE conditional function that can be used in that CMS.
  2. The function to get logged in user data and the function to output these data.

A quick search in Google can give you an answer for these functions and you can start building your code.

How did we do?

How to set up questions with a countdown

Create Dynamic Shopping Cart with JS and CSS

Contact