Skip to content

Instantly share code, notes, and snippets.

@nevilpaul
Last active September 11, 2018 00:36
Show Gist options
  • Save nevilpaul/e05d68aa33db075695182289b3d93452 to your computer and use it in GitHub Desktop.
Save nevilpaul/e05d68aa33db075695182289b3d93452 to your computer and use it in GitHub Desktop.
intercom - get basic data when your wordpress users are logged in and also when they are not logged simple code is added
/*
*@author nevilpaul..
*@mail: info@nevilpaul.com
*@date published:03/07/2018
*/
<?php
global $wp;
function intercom_script_footer(){
$ipaddress = '';
if ($_SERVER['HTTP_CLIENT_IP'])
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if($_SERVER['HTTP_X_FORWARDED_FOR'])
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if($_SERVER['HTTP_X_FORWARDED'])
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if($_SERVER['HTTP_FORWARDED_FOR'])
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if($_SERVER['HTTP_FORWARDED'])
$ipaddress = $_SERVER['HTTP_FORWARDED'];
else if($_SERVER['REMOTE_ADDR'])
$ipaddress = $_SERVER['REMOTE_ADDR'];
else
$ipaddress = 'UNKNOWN';
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user(); ?>
<script>
/* Show intercom widget */
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; ?>',
ip_address:'<?php echo $ipaddress;?>',
created_at: <?php echo strtotime(get_userdata($current_user->ID)->user_registered); ?>
};
<?php } else { ?>
<script>
//********* remove this script code if you want only intercom to be visible to user*************************
/* Show intercom widget */
window.intercomSettings = {
app_id: 'APP_ID',
ip_address:'<?php echo $ipaddress;?>'
};
//************************** end of remove *****************************************************************
<?php } ?>
/*Show intercom widget */
(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 }
/*script included when user is logged in*/
function is_page_to_include(){
intercom_script_footer();
};
// To add intercom to the front end of your wordpress site:
add_action('wp_footer', 'is_page_to_include');
//To add intercom to the admin area of your wordpress site:
add_action( 'admin_enqueue_scripts', 'is_page_to_include' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment