admin管理员组文章数量:1293466
Everywhere is said that you need to use wp_enqueue_scripts()
instead of wp_print_styles()
.
wp_print_styles()
is located in wp-includes/functions.wp-styles.php
.
So what does it mean? What should I change? Where? Why? Can it compromise my theme?
Everywhere is said that you need to use wp_enqueue_scripts()
instead of wp_print_styles()
.
wp_print_styles()
is located in wp-includes/functions.wp-styles.php
.
So what does it mean? What should I change? Where? Why? Can it compromise my theme?
Share Improve this question edited May 16, 2015 at 13:15 Sven 3,6841 gold badge35 silver badges48 bronze badges asked May 16, 2015 at 8:27 user72891user72891 2 |2 Answers
Reset to default 4In short, as of WordPress 3.3 use wp_enqueue_scripts
to load JS and CSS. wp_print_styles
has/had some minor bugs with it (namely, it may include your scripts in the admin as well) - here and here are some more details on all of that. It doesn't sound like there's any major security flaws or theme breaking here, just better practice to use wp_enqueue_scripts
.
Additionally:
- wp_enqueue_scripts - for enqueuing on the front end
- login_enqueue_scripts - for enqueuing on the login page
- admin_enqueue_scripts - for enqueuing on admin pages
The other answer confuses the function with the action hook. There is nothing wrong with using the wp_print_styles()
function to print a stylesheet link, as long as you've already registered/enqueued the stylesheet and know its handle.
(Fun fact: it's the function WP uses under the hood to output the enqueued stylesheets during wp_head()
.)
The guidance on the core blog and in the Trac ticket advises against using the wp_print_styles
hook to enqueue a stylesheet. In WP 3.3, this might have caused your custom stylesheet to load in the admin area, but this was fixed. Regardless, it's still the wrong hook for registering or enqueuing stylesheets, since it fires during the wp_print_styles()
function.
Generally, you should use wp_enqueue_style()
since it handles registration & enqueueing (and, as a result, printing the link as part of wp_head
). All-in-one simplicity.
However, if you need total control over where the stylesheet link is placed (say it needs to go in a specific template part, or wrapped inside <noscript>
tags), wp_print_styles()
is the right function to use.
Here's an example of how you might use them all:
// functions.php
function addMainStylesheet(){
wp_enqueue_style( 'main-styles', 'main.css' );
}
function registerNoscriptStylesheet(){
// registers the handle, but does not add it to the print queue.
wp_register_style( 'noscript-styles', 'noscript.css' );
}
add_action( 'wp_enqueue_scripts', 'addMainStylesheet' );
add_action( 'wp_enqueue_scripts', 'registerNoscriptStylesheet' );
// header.php
// Fire the `wp_head` hook, which prints the link for main.css
wp_head();
?>
<noscript>
<?php
// Now we'll print the handle we registered earlier
wp_print_styles( 'noscript-styles' );
?>
</noscript>
I'd recommend this question for more about the difference between wp_enqueue_style()
and wp_register_style()
:
Why wp_register_style() is important while I'm using a complete wp_enqueue_style()?
本文标签: cssUse wpenqueuescriptsnot wpprintstyles
版权声明:本文标题:css - Use wp_enqueue_scripts, not wp_print_styles? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741580041a2386515.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wp_prit_styles
we can tell you if you should change something or not. – cybmeta Commented May 16, 2015 at 8:33