[英]Enqueue Wordpress plugin scripts below all other JS
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
. 我正在开发一个简单的Wordpress应用程序,但我遇到了一个问题,因为所有插件脚本都是在我的
functions.php
中排队的那些之前呈现的。
Here is a sample section of the functions.php
: 以下是
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. 需要注意的重要一点是(按照最佳实践,我的JS设置为在页面底部呈现。
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 at the very bottom of the page? 如何强制Wordpress显示主JS(我相信
wp_head()
呈现在主页的最底部?
The WordPress wp_head() method will only output scripts or styles that have that last parameter in the WordPress wp_enqueue_script() set to false .. when it is set to true it will render in the footer via the wp_footer() WordPress的wp_head()方法将只显示有在WordPress的wp_enqueue_script()设置为false,最后一个参数。当它被设置为true,将在页脚通过wp_footer渲染输出脚本或样式()
you can change the priority of when its called and inserted by adjusting the $priority parameter in the add_action() 您可以通过调整add_action()中的$ priority参数来更改其调用和插入时的优先级
http://codex.wordpress.org/Function_Reference/add_action http://codex.wordpress.org/Function_Reference/add_action
$priority (int) (optional) Used to specify the order in which the functions associated with a particular action are executed.
$ priority(int)(可选)用于指定与特定操作关联的函数的执行顺序。 Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
较低的数字与先前的执行相对应,具有相同优先级的函数按照它们添加到操作的顺序执行。 Default: 10
默认值:10
add_action( $hook, $function_to_add, $priority, $accepted_args );
And also look at the following two WordPress methods: 并且还看看以下两个WordPress方法:
wp_enqueue_script() : wp_enqueue_script() :
https://developer.wordpress.org/reference/functions/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() : wp_register_script() :
https://developer.wordpress.org/reference/functions/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 this.. 尝试这个..
You might have to play with that $priority parameter 您可能必须使用该$ priority参数
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.