Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am calling a javascript file from my wordpress plugin code using:

add_action('init','gallery_sugar_js_init');

    function gallery_sugar_js_init() {
        wp_enqueue_script( 'gallery_sugar_js', plugins_url( '/js/gallery_sugar.js', __FILE__ ));
}

But in source code for WordPress I'm seeing this (my javascript file loading first):

<script type='text/javascript' src='http://localhost/projects/lightbox/wp-content/plugins/custom-gallery/js/gallery_sugar.js?ver=3.7'></script>
<script type='text/javascript' src='http://localhost/projects/lightbox/wp-includes/js/jquery/jquery.js?ver=1.10.2'></script>

How do I make sure jquery is loaded first in my Wordpress plugin file?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Try this:

add_action('init','gallery_sugar_js_init');

function gallery_sugar_js_init() {
  wp_enqueue_script( 'gallery_sugar_js', plugins_url( '/js/gallery_sugar.js', __FILE__ ), array( 'jquery' ) );
}

Check Function Reference for more info.

share|improve this answer

The Codex has a specific example exactly for this instance.

The third parameter in wp_enqueue_script is where you can define your dependencies. By putting jQuery as a dependency it will force jQuery to be loaded prior to the script you're enqueing. Here would be an example with your code:

wp_enqueue_script( 'gallery_sugar_js', plugins_url( '/js/gallery_sugar.js', __FILE__ ), array( 'jquery' ) );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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