Skip to content

Instantly share code, notes, and snippets.

@nevilpaul
Last active August 5, 2020 17:32
Show Gist options
  • Save nevilpaul/3cc09c01f626be229a814e1afab169e7 to your computer and use it in GitHub Desktop.
Save nevilpaul/3cc09c01f626be229a814e1afab169e7 to your computer and use it in GitHub Desktop.
PHP WITH INTERCOM SHOW INTERCOM AND FETCH USERDATA IN SPECIFIC WORDPRESS PAGES
/*
*@author nevilpaul..
*@mail: info@netdevelops.com
*@date published:03/07/2018
*/
<?php
global $wp;
function intercom_script_footer(){
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; ?>',
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'
};
//************************** 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 }
/*pages that intercom is being included to*/
function is_page_to_include(){
/* Replace these pages is where messenger is to be shown */
$urls_to_include = array(
"https://app.example.com/path/",
"https://app.example.com/path/",
"https://app.example.com/path/"
);
$url_to_check = home_url(add_query_arg(array(),$wp->request)).$_SERVER[REQUEST_URI];
if (in_array($url_to_check, $urls_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