javascript 将 Wordpress 插件脚本排在所有其他 JS 下方
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19913960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enqueue Wordpress plugin scripts below all other JS
提问by Sheixt
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()
呈现在页面的最底部显示的?
回答by Jonathan Marzullo
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 trueit 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 $priorityparameter 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. 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
$priority (int)(可选)用于指定与特定操作关联的函数的执行顺序。较低的数字对应于较早的执行,具有相同优先级的函数按照它们添加到操作的顺序执行。默认值: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 $priorityparameter
您可能必须使用该$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);