Scripts Enqueue Wordpress plugin under all other JS

I am developing a simple Wordpress application, but I have a problem, since all the plugin scripts are displayed before those that are queued in functions.php .

The following is an example functions.php section:

 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'); 

It is important to note that (in accordance with best practice, my JS is set up to render at the bottom of the page.

I also have a couple of plugins working on the topic. The problem is that the output is as follows:

 <!-- 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 do I get Wordpress to display the main JS (which, in my opinion, displays wp_head() to display at the very bottom of the page?

+6
source share
1 answer

The WordPress wp_head () method only outputs scripts or styles that have the last parameter in WordPress wp_enqueue_script () set to false .. when set to true , it will be displayed in the footer using wp_footer ()

you can change the priority when called and inserted by setting the $ priority parameter in add_action ()

http://codex.wordpress.org/Function_Reference/add_action

$ priority (int) (optional) Used to indicate the order in which the functions associated with a particular action are performed. Lower numbers correspond to earlier execution, and functions with the same priority are performed in the order in which they were added to the action. Default: 10

 add_action( $hook, $function_to_add, $priority, $accepted_args ); 

And also consider the following two WordPress methods:

wp_enqueue_script () :

https://developer.wordpress.org/reference/functions/wp_enqueue_script/

 wp_enqueue_script( string $handle, string $src = '', array $deps = array(), string|bool|null $ver = false, bool $in_footer = false ) 

wp_register_script () :

https://developer.wordpress.org/reference/functions/wp_register_script/

 wp_register_script( string $handle, string $src, array $deps = array(), string|bool|null $ver = false, bool $in_footer = false ) 

Try it.

You may have to play with the $ priority parameter

 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', 20, 1); 
+11
source

Source: https://habr.com/ru/post/957859/


All Articles