I’m no good at javascript but I think this code is right because it is just copy and paste from Zopim website and it was working before I tried to use their widget in a different approach.
functions.php
JavaScript
x
9
1
<?php
2
if ( is_product()) {
3
function wpa_enqueue_scripts() {
4
wp_enqueue_script( 'wpa-main-js', get_theme_file_uri( 'js/zopim.js' ), [], null, true );
5
}
6
7
add_action( 'wp_enqueue_scripts', 'wpa_enqueue_scripts', 100 );
8
}
9
I am having another issue with the code above. I can’t get the script file to load on products page. It only work if I remove the if conditional.
zopim.js
JavaScript
1
8
1
<script type="text/javascript">
2
window.$zopim||(function(d,s){var z=$zopim=function(c){z._.push(c)},$=z.s=
3
d.createElement(s),e=d.getElementsByTagName(s)[0];z.set=function(o){z.set.
4
_.push(o)};z._=[];z.set._=[];$.async=!0;$.setAttribute("charset","utf-8");
5
$.src="https://v2.zopim.com/?3eGZHNIx48cV45BpV2eeQ5nlDBvLzS0P";z.t=+new Date;$.
6
type="text/javascript";e.parentNode.insertBefore($,e)})(document,"script");
7
</script>
8
This script give me this error:
“Uncaught SyntaxError: expected expression, got ‘<‘” and I have no idea why.
Advertisement
Answer
“It only work if I remove the if conditional.”
- I’d write the
if
statement inside thewp_enqueue_scripts
hook. Like so:
JavaScript
1
10
10
1
add_action( 'wp_enqueue_scripts', 'wpa_enqueue_scripts');
2
3
function wpa_enqueue_scripts()
4
{
5
if ( is_product())
6
{
7
wp_enqueue_script( 'wpa-main-js', get_theme_file_uri( 'js/zopim.js' ), [], null, true );
8
}
9
}
10
“This script give me this error “Uncaught SyntaxError: expected expression, got ‘<‘” and I have no idea why.”
- You don’t need
script
tags in yourjavascript
file. So you could go ahead and remove<script type="text/javascript">
and</script>
.
Let me know if you could get it to work.