Skip to content

Instantly share code, notes, and snippets.

@JackGJenkins
Last active September 19, 2019 22:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JackGJenkins/039bdfce9c82cdc2eeb09df42d5079f3 to your computer and use it in GitHub Desktop.
Save JackGJenkins/039bdfce9c82cdc2eeb09df42d5079f3 to your computer and use it in GitHub Desktop.
This is some sample code that allows you to install Intercom via your child theme's functions.php file without the plugin.
<?php
global $wp;
function is_page_to_exclude(){
/* Replace these examples with any pages where you'd like the messenger to be hidden */
$urls_to_exclude = array(
"http://example.com/page-one",
"http://example.com/page-two",
"http://example.com/page-three"
);
$url_to_check = home_url(add_query_arg(array(),$wp->request));
if (in_array($url_to_check, $urls_to_exclude)) {
echo "TRUE";
}
};
function intercom_script_footer(){
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user(); ?>
<script>
/* Replace 'APP_ID' with your app ID */
window.intercomSettings = {
app_id: 'APP_ID',
email: '<?php echo $current_user->user_email; ?>',
user_id: '<?php echo $current_user->ID; ?>',
name: '<?php echo $current_user->display_name; ?>',
created_at: <?php echo strtotime(get_userdata($current_user->ID)->user_registered); ?>,
hide_default_launcher: <?php is_page_to_exclude(); ?>
};
<?php } else { ?>
<script>
/* Replace 'APP_ID' with your app ID */
window.intercomSettings = {
app_id: 'APP_ID',
hide_default_launcher: <?php is_page_to_exclude(); ?>
};
<?php } ?>
/*Replace 'APP_ID' with your app ID */
(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function") {ic('reattach_activator');ic('update',intercomSettings);}else{var d=document;var i=function(){i.c(arguments)};i.q=[];i.c=function(args) {i.q.push(args)};w.Intercom=i;function l(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/APP_ID';var x=d.getElementsByTagName('script') [0];x.parentNode.insertBefore(s,x);}if(w.attachEvent) {w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})()
</script>
<?php }
// To add intercom to the front end of your wordpress site:
add_action('wp_footer', 'intercom_script_footer');
//To add intercom to the admin area of your wordpress site:
add_action( 'admin_enqueue_scripts', 'intercom_script_footer' );
?>
@DannyOutlaw
Copy link

I have this in a child theme that I saw on marketplaces like Theme Forest, Etsy, Creative Market, etc.

It works great, but Intercom seems to treat every theme installation as a singular user. For example, if 5 people have purchased the theme, in Intercom I only see one user. That users name/email will changed based on the most recent customer to log into their website.

Just curious if you have run into this or if the code needs to be modified to treat each installation as a different user.

@JackGJenkins
Copy link
Author

@DannyOutlaw This isn't something I've ever seen, but it sounds like it's pulling the wrong info for user_id.

@nevilpaul
Copy link

so i do not have to install intercom plugin

@briantetrault
Copy link

Anyone been able to get user_hash to work? This code is great and works but can't get identity verification to work properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment