4

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?

2
  • 2
    It depends on what you want to do. I you can post more details of the use you are doing of wp_prit_styles we can tell you if you should change something or not. – cybmeta May 16 '15 at 8:33
  • My problem is simply that some css are not rendered. In the console they appear inline but they are not, so I guess it's because it's ouput directly to the browser via wp_print_styles. – user72891 May 17 '15 at 7:27
4

In 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:

0

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()?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy