最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - How do I force wp_enqueue_scripts to load at the END of <head>?

programmeradmin0浏览0评论

I am loading scripts via wp_enqueue_scripts in my child theme.

The only problem is that my style.css script get loaded BEFORE plugin scripts, yet I need to override the CSS in the plugins with my style.css. So it needs to load AFTER plugin scripts.

I don't want to add "!important" too all of my CSS styles because that is tedious and could break some of my styling.

I tried adding a priority but it did not effect the loading order at all.

    add_action('wp_enqueue_scripts', 'scripts', 9999);

I would simply like to wp_enqueue_scripts at the very end of the header, just before </head>. How can it be done? Thanks :-)

UPDATE:

Yay I figured it out!

Simply register the filter with:

    add_action('wp_print_styles', 'scripts');

wp_print_styles loads with a priority of 8, and it still places them in the header because it's a part of wp_head.

I figured this out by looking in wp-includes/default-filters.php, so thanks for the tip! :-)

I am loading scripts via wp_enqueue_scripts in my child theme.

The only problem is that my style.css script get loaded BEFORE plugin scripts, yet I need to override the CSS in the plugins with my style.css. So it needs to load AFTER plugin scripts.

I don't want to add "!important" too all of my CSS styles because that is tedious and could break some of my styling.

I tried adding a priority but it did not effect the loading order at all.

    add_action('wp_enqueue_scripts', 'scripts', 9999);

I would simply like to wp_enqueue_scripts at the very end of the header, just before </head>. How can it be done? Thanks :-)

UPDATE:

Yay I figured it out!

Simply register the filter with:

    add_action('wp_print_styles', 'scripts');

wp_print_styles loads with a priority of 8, and it still places them in the header because it's a part of wp_head.

I figured this out by looking in wp-includes/default-filters.php, so thanks for the tip! :-)

Share Improve this question edited Dec 16, 2019 at 14:10 butlerblog 5,1213 gold badges28 silver badges44 bronze badges asked Jul 4, 2012 at 3:55 NateNate 811 gold badge1 silver badge3 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 5

wp_enqueue_scripts is added per default with a priority of 1 to wp_head. See wp-includes/default-filters.php for details.

You can try to change the priority:

remove_action( 'wp_head', 'wp_enqueue_scripts', 1 );
add_action( 'wp_head', 'wp_enqueue_scripts', 9999 );

But I don’t recommend it. There is probably a good reason for the default value. Some scripts may not work anymore when you change this.

I think the more WordPress friendly way to do this is to use wp_enqueue_styles()'s $deps parameter. Assuming the plugin styles are enqueued via wp_enqueue_styles() (which, admittedly, pathetically few are), you list an array of the stylesheet handles that your styles depend on and then they load afterwards.

发布评论

评论列表(0)

  1. 暂无评论