[ad_1]
I’ve worked on this for days and made little progress because I suck at javascript.
Goal: Create a wp plugin that allows choosing an alternate chat department for each web page, or use the default chat dept for the domain. I can do all of this with php in my plugin, BUT… all of my WordPress websites are using Zoho SalesIQ plugin. I cannot disable this plugin, so instead I must edit the chat widget javascript that’s in the plugin settings, so NO PHP. Yuck.
Much research lead me to a potential solution – I’ve inserted custom meta tags into the head that will hold the correct data to insert into the javascript. But that’s where I get lost – how do I replace these two values in the zoho javascript with the content from the custom meta tags?
<meta name="my_zoho_widgetcode" content="123456789" />
<meta name="my_zoho_deptcode" content="deptA" />
<script id="zsiqchat">
var $zoho=$zoho || {};
$zoho.salesiq = $zoho.salesiq || {
widgetcode: "GET-WIDGETCODE-FROM-META-TAG",
values:{},
ready:function(){}
};
var d=document;
s=d.createElement("script");
s.type="text/javascript";
s.id="zsiqscript";
s.defer=true;
s.src="https://salesiq.zoho.com/widget?plugin_source=wordpress";
t=d.getElementsByTagName("script")[0];
t.parentNode.insertBefore(s,t);
$zoho.salesiq.ready=function() {
$zoho.salesiq.chat.department("GET-DEPT-FROM-META-TAG");
};
</script>
I managed to get it to work this way, but it’s going to require a lot of IF conditions (shortened here), so I’m looking for a cleaner solution.
<script type="text/javascript" id="zsiqchat">
var zoho_meta_widgetcode = document.querySelector('meta[name="zoho_widgetcode"]').content;
var zoho_meta_deptcode = document.querySelector('meta[name="zoho_deptcode"]').content;
var $zoho=$zoho || {};
if (zoho_meta_widgetcode === '123456789') {
$zoho.salesiq = $zoho.salesiq || {
widgetcode:"123456789",
values:{},ready:function(){}
};
}
if (zoho_meta_widgetcode === '') {
$zoho.salesiq = $zoho.salesiq || {
widgetcode:"000000000",
values:{},ready:function(){}
};
}
var d=document;
s=d.createElement("script");
s.type="text/javascript";
s.id="zsiqscript";
s.defer=true;
s.src="https://salesiq.zoho.com/widget?plugin_source=wordpress";
t=d.getElementsByTagName("script")[0];
t.parentNode.insertBefore(s,t);
$zoho.salesiq.ready=function()
{
if (zoho_meta_deptcode === 'deptA') {
$zoho.salesiq.chat.department(["deptA"]);
}
if (zoho_meta_deptcode === '') {
$zoho.salesiq.chat.department(["Default"]);
}
};
</script>
[ad_2]