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应用程序,但是我有一个问题,因为所有的插件脚本都是在我的函数中加入的。
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.
需要注意的重要一点是(按照最佳实践,我的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()呈现在页面底部的?
1 个解决方案
#1
14
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. 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 $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);
#1
14
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. 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 $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);