0

So I've got a bit of an issue, and not sure what the problem is. I'm trying to enqueue javascript in the footer of my Wordpress site. The footer looks like this:

    </footer>
</div>

<?php wp_footer(); ?>

<script>
    jQuery(document).foundation();
</script>
</body>
</html>

So the enqueued script SHOULD be put in the wp_footer call, putting the script just above where I actually call it. But looking at the source code of the file, I get this:

</footer>
</div>







<script>
jQuery(document).foundation();
</script>
<script type='text/javascript' src='http://www.thetestsite.com/wp-content/themes/cornerstone-4.3.1/js/foundation.min.js?ver=4.3.3'></script>
</body>
</html>

For some reason, the script in question is being placed RIGHT BEFORE the closing body tag.

I verified the wp_footer is in the correct place, so I'm not sure what would cause this to happen. If I enqueue the script to the head, it works just fine, but I'm just curious as to why this behavior is happening.

UPDATE (NEW CODE): Sorry for the confusion. Here's the functions.php portion of the script enqueue method:

function load_cornerstone_scripts() {
    wp_enqueue_script(
        'foundation_js',
        get_template_directory_uri() . '/js/foundation.min.js',
        array('jquery'),
        '4.3.1',
        true
    );

}

add_action('wp_enqueue_scripts', 'load_cornerstone_scripts',0);

So to be clear..the original two blocks of code are showing 1) What the php file footer.php looks like, and 2) what the source code is rendered as.

5
  • 1
    I am not seeing any issue. In your code your wp_footer() function IS placed before your closing body tag so it is correct that your enqueue will also show before the closing body tag...What is the issue there?
    – user23654
    Sep 4, 2013 at 16:00
  • If you look at the code, the wp_footer code should execute, and THEN my jQuery(document).foundation(); script should fire. As it is, the enqueued script is being put AFTER my jQuery(document).foundation(); script call, which is causing a javascript error because the foundation function hasn't been declared yet. Sep 4, 2013 at 16:16
  • But it is contradicting. In one instance you're enqueuing the script and the other you're hard coding it in your footer...Why not enqueue both script one after the other or even with dependencies?
    – user23654
    Sep 4, 2013 at 16:30
  • It's the same thing. The top one is how my php code looks, and the bottom is how it's rendered. According to my php code, the cornerstone script SHOULD be loaded before my jquery function call. but in the second code block, the cornerstone is being put AFTER my jquery function call. The second code block is the resulting code when php is parsed and html is generated. Sep 4, 2013 at 18:43
  • Are you sure you're working on the correct theme files? Are you sure it is not cached or something like that?
    – hamdirizal
    Apr 28, 2019 at 6:53

1 Answer 1

0

You want something like this.

// assuming you want to load this only on frontend
if ( ! is_admin() )
     add_action( 'wp_enqueue_scripts', 'wpse_112876_load_scripts' );

function wpse_112876_load_scripts() {
     wp_enqueue_script( 'my_foundation', get_template_directory_uri() . '/js/foundation.min.js', null, '4.3.3', true );

     wp_enqueue_script( 'my_foundation_init', get_template_directory_uri() . '/js/foundation_init.js', array( 'my_foundation' ), null, true );
}
3
  • Thanks for that, but that's not the problem I'm having. I am enqueueing my script. For some reason, though, it's not putting it in the wp_footer() call and I don't know why. Sep 4, 2013 at 18:50
  • Again, I am telling you to enqueue both scripts and not have one with enqueue and the other hardcoded.
    – user23654
    Sep 4, 2013 at 18:52
  • There's only one script file, and that's the minified foundation.js file. The other piece is the "activation" of it. You're right, I COULD just take that function call and put it inside the script...or I could enqueue it to the header, which would fix the problem. But I'm not trying to fix the problem like that...I'm trying to understand the problem of why the script is being added UNDER the function call, when it should be added above it (inside the wp_footer(); action). Sep 4, 2013 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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