2

I am developing a simple Wordpress app but I am having an issue as all of the plugin scripts are rendered before those which are enqueued in my functions.php.

Here is a sample section of the functions.php:

function my_scripts() {
    wp_register_script('app-js', get_template_directory_uri() . '/javascripts/app.js', array('jquery'), null, true );
    wp_enqueue_script('app-js');
}
add_action( 'wp_enqueue_scripts', 'my_scripts');

The important this to note is that (following best practice my JS is set to render at the bottom of the page.

I also have couple of plugins running on the theme. The problem is that the output looks like this:

<!-- Note the plugin appears first -->
<script type='text/javascript' src='http://localhost/wordpress/wp-content/plugins/my-plugin/acl-plugin.js'></script>
<!-- And the main JS appears second -->
<script type='text/javascript' src='http://localhost/wordpress/wp-content/themes/my-theme/javascripts/app.js'></script>
</body>
</html>

How can I force Wordpress to display the main JS (which I believe is rendered by wp_head() ) to appear above the plugin scripts? That way the plugins can reference JS functions that are in the main theme.

5
  • Try and add the priority parameter to the hook for app.js and see if it works. like add_action( 'wp_enqueue_scripts', 'my_scripts', 1 );. By default it is 10, so lets call it early and see if this works for you Commented Nov 11, 2013 at 19:37
  • your question is a bit confusing, your app.js is at the bottom of the page (output by wp_footer, not wp_head), what's the issue?
    – Milo
    Commented Nov 11, 2013 at 19:40
  • Woops, type-o in there. Thanks @Milo what I meant was how to I ensure that the PLUGINS appears below the main script. I'll edit the question now.
    – Sheixt
    Commented Nov 11, 2013 at 19:51
  • 1
    @MarutiMohanty thanks for the suggestion. Adding the priority has moved it! Do you want to add this as an answer (so you get the rep) or I can outline the answer for others instead.
    – Sheixt
    Commented Nov 11, 2013 at 20:05
  • @Sheixt sure, done :) Commented Nov 11, 2013 at 20:14

1 Answer 1

3

Try and use/add the priority parameter of the hook for app.js and see if it works.

add_action( 'wp_enqueue_scripts', 'my_scripts', 1 );

By default it is 10, so lets call it early and see if this works for you

Your Answer

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

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