0

Have a quick question,

Below code is a simple function for enqueue the scripts in WordPress, but the problem is with JS, not PHP. At this state it gives an error:

Missing ) after argument list

I believe it is a problem with my method of escaping the quotes, since using this script manually by adding to the footer works OK.

add_action('wp_enqueue_scripts', function () {
    wp_enqueue_style('sage/main.css', asset_path('styles/main.css'), false, null);
    wp_enqueue_script('sage/main.js', asset_path('scripts/main.js'), ['jquery'], null, true);
    $inline_js = 'var $zoho=$zoho || {};$zoho.salesiq = $zoho.salesiq || {widgetcode:"a7d50e2c5dd79539b3393dbeaf47ed569e663414dbe68c618856655be9267b02", 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";t=d.getElementsByTagName("script")[0];t.parentNode.insertBefore(s,t);d.write("<div id=\'zsiqwidget\'></div>")';
    wp_add_inline_script('sage/main.js', $inline_js);
}, 100);

What I am missing here? Big thanks for the help!

  • 1
    In PHP anything within single quotes is taken literally. I'd try changing it to $inline_js = "var ... and escaping the double quotes instead of the single quotes. – WebElaine Jun 27 '17 at 17:46
  • What is $zoho? Is that a PHP variable? – Spartacus Jun 27 '17 at 20:33
0

Since you are using dollar sign in conjunction with some other variables, I assume you are trying to get a php variable inside your inline script. You will have 3 options.

Closing and opening the quotes

You can echo a variable in this way:

echo 'Hello' . $world;

Using double quotes

The same above code can be written in this way:

echo "Hello $world";

Pay attention to the double quotes.

Using curved brackets & double quotes

This is the cleanest way to do it, since it makes reading the code much easier:

echo "Hello {$world}";

So, your code can be turned into this:

add_action('wp_enqueue_scripts', function () {

    wp_enqueue_style('sage/main.css', asset_path('styles/main.css'), false, null);
    wp_enqueue_script('sage/main.js', asset_path('scripts/main.js'), ['jquery'], null, true);

    $inline_js = "
    var {$zoho} = {$zoho} || {};
    {$zoho}.salesiq = {$zoho}.salesiq || {
        widgetcode:'a7d50e2c5dd79539b3393dbeaf47ed569e663414dbe68c618856655be9267b02', 
        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';
    t=d.getElementsByTagName('script')[0];
    t.parentNode.insertBefore(s,t);
    d.write('<div id=\'zsiqwidget\'></div>')";

   wp_add_inline_script('sage/main.js', $inline_js);
}, 100);

I also formatted the code so it's easier to be read.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.