• Resolved crazypatx

    (@crazypatx)


    Hi All – I am seeking help as I can not get the JS snippet running as expected.

    I created a JS snippet, set it to auto insert, select site wide footer and everything else is default. The snippet is active.
    When loading the page, I can see that the function is shown in the index file (via developer tools sources) but it is NOT executed (console log doesn’t show).
    function JSsnippet1(){
    console.log('Test JSsnippet1#');
    }

    Then I added a PHP snippet, set it to auto insert, run everywhere and everything else is default. The snippet is active.
    When loading the page, I can see that the function is shown in the index file (via developer tools sources) and it works properly! (console log is shown).
    add_action("wp_footer", "JSsnippet2", 999999);
    function JSsnippet2(){
    ?>
    "script" console.log('Test JSsnippet1#'); "script"
    <?php
    }

    Note: I can not copy the “script” tags in here, in my code they are correct with <>

    Does one of you have any idea why the JS snippet does not work but the PHP snippet does?

    • This topic was modified 1 year, 6 months ago by crazypatx.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Mircea Sandu

    (@gripgrip)

    Hi @crazypatx,

    Is the snippet being output with proper script tags when using the JS snippet type? The first thing that comes to mind is to check if maybe you are using a performance or cookie consnet plugin that might be attempting to delay when the script is being executed?

    Thread Starter crazypatx

    (@crazypatx)

    Hi @gripgrip – thanks for your help!

    I am not sure if I fully get your question.
    Checking the index files source code, the JS is properly included (embedded in script /script section).

    I do not have any consent plugins and I also tried to delay the execution of the script within the JS function itself – this did not work either.

    Plugin Author Mircea Sandu

    (@gripgrip)

    Hi @crazypatx,

    I’m sorry about the confusion here – I can see now the issue. In your first example of code, you are not actually calling the JSsnippet1 function while in the second one you are calling the PHP function that directly outputs the console.log. The solution in this case should be to add a call to the JSsnippet1 function at the end of the code JS code:

    JSsnippet1();
    Thread Starter crazypatx

    (@crazypatx)

    Thanks @gripgrip – this works!
    I somehow thought that setting the snippet to “site wide footer” is replacing the “add_action” statement and automatically calling all the included functions.
    but now adding the explicit function call everything works.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘JS snippet auto insert in footer does not work’ is closed to new replies.