Thursday, January 27, 2022

[FIXED] Can't get variables to work in Wordpress for Intercom Javascript

Issue

Intercom provide the following snippet to be injected before the body tag:

<script>
  window.intercomSettings = {
    app_id: "",
    name: "<?php echo json_encode($current_user->name) ?>", // Full name
    email: "<?php echo json_encode($current_user->email) ?>", // Email address
    created_at: "<?php echo strtotime($current_user->created_at) ?>" // Signup date as a Unix timestamp
  };
</script>

However, it creates the launcher with an error; stating to check the email variable. What should the variable be for Wordpress?


Solution

It's not meant to be injected before body, it has to be before the body close tag .

<script>
  window.intercomSettings = {
    app_id: "",
    name: "<?=wp_get_current_user()->user_login?>", // Full name
    email: "<?=wp_get_current_user()->user_email?>", // Email address
    created_at: "<?=strtotime(wp_get_current_user()->created_at)?>" // Signup date as a Unix timestamp
  };
</script>
</body>


Answered By - TheDon

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.