Loading CSS Into WordPress the Right Way

Without CSS, you have very limited choices to style your web pages. And without proper CSS inclusion inside WordPress, you can make it extremely hard for your theme's users to customize the theme's styling. 

In this tutorial, we're going to have a look at the right way to enqueue CSS into WordPress.

WordPress is currently the most popular content management system in the world, and it has tens of millions of users. That's why, in order to make a successful theme, we need to think as every single WordPress user and try to go by the book and load our CSS files in our themes properly.

The Wrong Way to Load CSS in WordPress

Over the years, WordPress has grown its code in order to make it more and more flexible, and enqueueing CSS and JavaScript was a move on that direction. Our bad habits remained for a while, though. With knowing that WordPress introduced CSS and JavaScript enqueueing, we continued to add this code into our header.php files:

...or we added the code below into our functions.php files, thinking it was better:

In the cases above, WordPress can't determine whether the CSS files are loaded in the page or not. That might be an awful mistake! 

If another plugin uses the same CSS file, it wouldn't be able to check if the CSS file has already been included in the page. Then the plugin loads the same file for a second time, resulting in duplicate code.

Luckily, WordPress has a pretty easy solution to problems like this: registering and enqueueing stylesheets.

The Right Way to Load CSS in WordPress

As we said earlier, WordPress has grown a lot over the years and we have to think about every single WordPress user in the world. 

In addition to them, we also have to take thousands of WordPress plugins into account. But don't let these big numbers scare you: WordPress has pretty useful functions for us to properly load CSS styles into WordPress. 

Let's have a look.

Registering the CSS Files

If you're going to load CSS stylesheets, you should register them first with the wp_register_style() function:

  • $handle (string, required) is unique name for your stylesheet. Other functions will use this "handle" to enqueue and print your stylesheet.
  • $src (string, required) refers to the URL of the stylesheet. You can use functions like get_template_directory_uri() to get the style files inside your theme's directory. Don't ever think about hard-coding it!
  • $deps (array, optional) handles names for dependent styles. If your stylesheet won't work if some other style file is missing, use this parameter to set the "dependencies".
  • $ver (string or boolean, optional) is the version number. You can use your theme's version number or make up one, if you want. If you don't want to use a version number, set it to null. It defaults to false, which makes WordPress add its own version number.
  • $media (string, optional) is the CSS media types like "screen" or "handheld" or "print". If you're not sure you need to use this, don't use it. It defaults to "all".

Here's an example to the wp_register_style() function:

Registering styles is kind of "optional" in WordPress. If you don't think your style is going to be used by any plugin or you're not going to use any code to load it again, you're free to enqueue the style without registering it. See how it's done below.

Enqueueing the CSS Files

After registering our style file, we need to "enqueue" it to make it ready to load in our theme's <head> section. 

We do this with the wp_enqueue_style() function:

The parameters are exactly the same with the wp_register_style() function, so no need for repeating them. 

But as we said the wp_register_style() function isn't mandatory, I should tell you that you can use wp_enqueue_style() in two different ways:

Keep in mind that if a plugin will need to find your stylesheet or you intend to load it in various parts in your theme, you should definitely register it first.

Loading the Styles Into Our Website

We can't just use the wp_enqueue_style() function anywhere in our theme – we need to use "actions". There are three actions we can use for various purposes:

Here are the examples for these three actions:

There's an important announcement in Make WordPress: "Use wp_enqueue_scripts(), not wp_print_styles()!". It tells you about a possible incompatibility error with WordPress v3.3.

Some Extra Functions

There are some very useful functions about CSS in WordPress: They allow us to print inline styles, check the enqueue state of our style files, add meta data for our style files, and deregister styles. 

Let's have a look.

Adding Dynamic Inline Styles: wp_add_inline_style()

If your theme has options to customize the styling of the theme, you can use inline styling to print them with the wp_add_inline_style() function:

Quick and easy. Remember though: You have to use the same hadle name with the stylesheet you want to add inline styling after.

Checking the Enqueue State of the Stylesheet: wp_style_is()

In some cases, we might need the information on a style's state: Is it registered, is it enqueued, is it printed or waiting to be printed? You can determine it with the wp_style_is() function:

Adding Meta Data to the Stylesheet: wp_style_add_data()

Here's an awesome function called wp_style_add_data() which allows you to add meta data to your style, including conditional comments, RTL support and more! 

Check it out:

Awesome, isn't it?

[If I'm not mistaken, this is the first tutorial ever written about this little—but useful—function.

Deregister Style Files with wp_deregister_style()

If you ever need to "deregister" a stylesheet (in order to re-register it with a modified version, for example), you can do it with wp_deregister_style()

Let's see an example:

Although it's not required, you should always re-register another style if you deregister one – you might break something if you don't.

[There's also a similar function called wp_dequeue_style(), which removes the enqueued stylesheets as its name suggests.

Wrapping Everything Up

Congratulations, now you know everything about including CSS in WordPress correctly! Hope you enjoyed the tutorial.

Do you have any tips or experiences you want to share? Comment below and share your knowledge with us! And if you liked this article, don't forget to share it with your friends!

Tags:

Comments

Related Articles