Stylesheets are at the heart of making HTML pages look good, in WordPress and the web. And the way that we make CSS stylesheets load into the page in WordPress is using the wp_enqueue_style
function. It’s not too complicated, but there a few common places that people new to using it will get tripped up. This Quick Guide will cover those, and then get you running in a step-by-step guide.
Understanding WordPress: wp_enqueue_style in More Depth
If you’re new to WordPress development, you may not be super familiar to the concept of WordPress specific functions. wp_enqueue_style
is a common example of just such a WordPress-provided function. Though the name’s a little weird, it basically just means “tell WordPress to load a stylesheet.”
Here’s the video explaining more about loading in stylesheets in WordPress:
wp_enqueue_style
Example Code to Load Your Stylesheet, Nothing Else
This code if directly from the video above, but reformatted with added comments. For me in the video, this code lives is in a plugin.
If you put this code in a theme, the biggest change you’ll need to make is $file_url
will probably get defined using something like get_stylesheet_directory_uri
instead.
// This line runes our function when
// WordPress hits the wp_enqueue_script
// milestone of loading
add_action(
'wp_enqueue_scripts',
'wpshout_enqueue_styles'
);
// This function runs because of the add_action
function wpshout_enqueue_styles() {
// Location of our stylesheet
// For me, this shared a plugin
// with this code
$file_url = plugins_url(
'stylesheet.css', // File name
__FILE__ // Magic PHP constant that means
// the "current file"
);
// Actually load up the stylesheet
wp_enqueue_style(
'sp_sytlesheet', // A "name" for our file
$file_url // Location variable
);
}
If everything in the code made sense to you, you can probably stop reading and copy-and-paste it. It will work fine. If not, please keep reading, we’ve got a great explanation for you. 😊
Which WordPress Hook to Use with wp_enqueue_style
If you watched the video, you may have already caught it, but for wp_enqueue_style
, we’ve got to use a “WordPress hook” as well. We’ve got way more details about WordPress hooks generally in this great article:
WordPress Hooks, Actions, and Filters: What They Do and How They Work
If you want even more depth about them, we’ve got a Course of free articles that goes deep. Everything from registering your own hooks to de-registering others:
https://wpshout.com/course-complete-introduction-wordpress-hooks-system/
wp_enqueue_style for Styling Your WordPress Site
- Your first line of code is an
add_action
call which is used to make sure that WordPress runs the code that really contains your use of the enqueuing function. That looks something likeadd_action( 'wp_enqueue_scripts', 'function_name' );
. The second function name will match function you create in #2. - Create a wrapper function for your use of
wp_enqueue_style
. For us, if you copied the line above, it would befunction_name
. I recommend a more descriptive name though. 🤓 - Inside of that function, you’ll need to locate the URL for your CSS stylesheet. For me, that meant getting the URL of the plugins directory using
plugins_url
, and then passing it the right arguments for it to produce the URL. You’ll probably need a different function if you’re in a theme. - Finally, we pass it all to the
wp_enqueue_stylesheet
function from WordPress. Pretty quick and easy. We must define two arguments, though the function can take more. The ones I do in my example code are the (1) “handle” or nickname for WordPress to use with our stylesheet, and (2) the actual URL (or web address) of our CSS file.
Can you make wp_enqueue_style
Async?
Being a Quick Guide, the topic of making wp_enqueue_style
asynchronous is a little too ambitious for me to give you a final answers to. We have lots of experience and a great article about making wp_enqueue_script
for JavaScript files asynchronous, which is common and supported well by browsers. Check that out:
For CSS, it’s less common and support to load it out-of-band. Mostly, this is because most CSS must be loaded for a browser to start “painting” the web page’s content. Here’s a useful article from CodePen about why it’s not common. You can accomplish this approximate goal with JavaScript, which this article explain. And if you only need to support cutting-edge browser, preload
of link
s is slowly getting supported by more of them. Can I Use has the latest statistics on <link rel=”preload”>
support across browsers.
I’m trying to include a css after the wordpress css but my code does not work.
wp_enqueue_style (‘bootstrap_mega_importer’, array (‘common’, ‘forms’), false, ‘all’);