簡體   English   中英

將Wordpress插件腳本排在所有其他JS下面

[英]Enqueue Wordpress plugin scripts below all other JS

我正在開發一個簡單的Wordpress應用程序,但我遇到了一個問題,因為所有插件腳本都是在我的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');

需要注意的重要一點是(按照最佳實踐,我的JS設置為在頁面底部呈現。

我也有幾個插件在主題上運行。 問題是輸出看起來像這樣:

<!-- 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>

如何強制Wordpress顯示主JS(我相信wp_head()呈現在主頁的最底部?

WordPress的wp_head()方法將只顯示有在WordPress的wp_enqueue_script()設置為false,最后一個參數。當它被設置為true,將在頁腳通過wp_footer渲染輸出腳本或樣式()

您可以通過調整add_action()中$ priority參數來更改其調用和插入時的優先級

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

$ priority(int)(可選)用於指定與特定操作關聯的函數的執行順序。 較低的數字與先前的執行相對應,具有相同優先級的函數按照它們添加到操作的順序執行。 默認值:10

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

並且還看看以下兩個WordPress方法:

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 )

嘗試這個..

您可能必須使用該$ 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM