Last active
September 19, 2019 22:06
-
-
Save thewheat/887c6009c8058e8140a0f8c086e3b841 to your computer and use it in GitHub Desktop.
Intercom Wordpress plugin customisations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////////////////////////////////////////////////////////////////////////////////////////////////// | |
// Customise functionality of the default Intercom Wordpress plugin | |
// https://github.com/intercom/intercom-wordpress | |
//////////////////////////////////////////////////////////////////////////////////////////////////// | |
// This code can be added to a plugin or a theme | |
// Simplest install would be in your theme's function.php file | |
//////////////////////////////////////////////////////////////////////////////////////////////////// | |
// customise the current Intercom script | |
// - add custom attributes https://docs.intercom.io/configuring-intercom/send-custom-user-attributes-to-intercom | |
// - add custom activator https://docs.intercom.io/configuring-Intercom/in-app-messenger#custom-style | |
function customise_intercom(){ | |
// $current_user = wp_get_current_user(); // get current Wordpress user if needed | |
echo "\n".'<script>'; | |
// ensure this is defined, i.e. don't break if Intercom plugin not installed | |
echo "\n".'window.intercomSettings = window.intercomSettings || {};'; | |
// specify custom attributes | |
// echo "\n".'window.intercomSettings["test_string"] = "123";'; | |
// echo "\n".'window.intercomSettings["test_string"] = "' . encodeAppropriately($current_user->display_name). '";'; // encodeAppropriately() is a custom function you will need to write or replace and is to ensure that it doesn't break the Javascript code. Escaping double quotes should be sufficient | |
// widget activator old messenger | |
// echo "\n".'window.intercomSettings.widget = {"activator":"#idOfActivatorElement"};'; | |
/////////////////////////////////////////////////////////////////////////////////////////// | |
// new messenger customizations | |
// https://docs.intercom.com/configure-intercom-for-your-product-or-site/customize-the-intercom-messenger/customize-the-intercom-messenger-technical | |
/////////////////////////////////////////////////////////////////////////////////////////// | |
// use a customer element as a launcher | |
// echo "\n".'window.intercomSettings["custom_launcher_selector"] = "#idOfActivatorElement";'; | |
// hide the messenger | |
// echo "\n".'window.intercomSettings["hide_default_launcher"] = true;'; | |
echo "\n".'</script>'."\n"; | |
} | |
// priority 20 ensures it loads after Intercom plugin which users the default value of 10 | |
// https://developer.wordpress.org/reference/functions/add_action/ | |
// https://github.com/intercom/intercom-wordpress/blob/master/bootstrap.php | |
add_action('wp_footer', 'customise_intercom',20); | |
//////////////////////////////////////////////////////////////////////////////////////////////////// | |
// add Intercom to admin dashboard i.e. /wp-admin/ interface - 1 | |
add_action('admin_footer', 'add_intercom_snippet'); | |
add_action('admin_footer', 'customise_intercom'); | |
// add Intercom to admin dashboard i.e. /wp-admin/ interface - 0 | |
//////////////////////////////////////////////////////////////////////////////////////////////////// |
Thanks a lot. This is exactly what I was after. I do not understand why they've left some many options out in the official plugin.
Hello @thewheat ,
first, congratulations, incredible code.
See if you can help me, I would like to add INTERCOM only to the BACK-END of wordpress, can you help me?
I've already tried removing the intercom via CSS in the FRONT-END, but it keeps getting visits to INTERCOM, even without the button on the front end.
Thank you very much! It will be a great help!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adding customisations to a theme's functions.php file via Editor in Wordpress (this is only available on certain configurations, but this feature is likely disabled as it can cause a Wordpress site to go down with an incorrect edit)
functions.php file in Wordpress directory structure