• Hi All,

    I am trying to load a javascript just before the closing body tag. I have created a functions.php file and a javascript file (in a folder called js) in my child theme folder.

    The functions.php code is as follows:


    <?php
    function example_function() {
    //Load JS and CSS files in here
    wp_register_script ('example_script', get_template_directory_uri() . '/js/example_script.js', array( 'jquery' ),'1', true);

    wp_enqueue_script('example_script');
    }

    add_action( 'wp_enqueue_scripts', 'example_function' );
    ?>

    The code of example_script.js is as follows:


    <!-- Quantcast Tag, part 2 -->
    <!-- hide script from old browsers
    _qevents.push( { qacct:"XXXXXXXXXX"} );
    <noscript>
    <div style="display: none;">
    <img src="https://pixel.quantserve.com/pixel/p-test123.gif" height="1" width="1" alt="Quantcast"/>
    </div>
    </noscript>
    // end hiding script from old browsers -->

    From the research I did online, this should be working but, when I check the source code of my homepage, the javascript code is nowhere to be seen.

    I would greatly appreciate if someone could review my code and advise me if I have made an error.

    Thank you in advance.

    Chris

Viewing 4 replies - 1 through 4 (of 4 total)
  • I had a problem with registering scripts recently. It turned out to be the ‘get_template_directory_uri()’ part of the function. That is giving the location of the parent theme.

    Try replacing with ‘get_stylesheet_directory_uri()’, which is relative to the child theme.

    You are registering the script dependent on jQuery.

    Are you sure jQuery is being loaded on the page where you are including the script?

    Check your browser console for errors.

    Thread Starter westcoastjetfighters

    (@westcoastjetfighters)

    mrkred, Josh,

    Thank you for your help. I did what you both suggested. I checked the Firebug console and found a 404 error for the external javascript file. I used get_stylesheet_directory_uri() instead of get_template_directory_uri() and that error disappeared. I then had an error for the javascript contained in the external file telling me <noscript> was incorrect. I did some more online research and changed the code of the external javascript file to this:


    <!-- Quantcast Tag, part 2 -->
    <!-- hide script from old browsers
    _qevents.push({qacct: "XXXXXXXXXX"});
    document.write("<noscript><div style=\"display: none;\"><img src=\"https://pixel.quantserve.com/pixel/p-test123.gif\" height=\"1\" width=\"1\" alt=\"Quantcast\"/></div></noscript>");
    // end hiding script from old browsers -->

    I thought this was going to fix it but the script is still not showing. The console is not showing any errors anymore.

    Any other ideas?

    Thread Starter westcoastjetfighters

    (@westcoastjetfighters)

    Update: I had flushed all caches and hard refreshed the browser but, somehow, it must have using a cached version of the page. The script is now inserted before the closing body tag.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘wp_enqueue_script not working’ is closed to new replies.