Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress How to Build a WordPress Theme Preparing to Code WordPress Templates Linking CSS

Luke Travis
Luke Travis
2,887 Points

$handle parameter of wp_enqeue_style() function

I don't understand exactly what exactly the $handle parameter is in relation to the wp_enque_style() function. Is the handle parameter defining a handle for the stylesheet, or is it calling a preexisting handle that is defined somewhere else? Also, when using the get_template_directory_uri() as the src parameter, how does the get_template_directory_uri() know to grab from the css directory

3 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Luke,

The $handle parameter is simply the name of script to want to queue, the name can be anything you want as it's simply a way for WordPress to track your script along the way so if you need to dequeue the script you can and change it to something else.

http://codex.wordpress.org/Function_Reference/wp_enqueue_style#Examples

Luke Travis
Luke Travis
2,887 Points

thanks for the response Chris. I guess my real question is, say I use the function wp_enqueue_style('main', get_template_directory_uri() . '/style.css'); If I had used wp_register_style('main', get_template_directory_uri() . '/style.css'), then I would have only needed to use one parameter when using the wp_enqueue_style() script. However, seeing as I never used wp_register_style before using wp_enqueue_style(), does the wp_enqueue_style() function do double duty of registering a stylesheet AND queueing it up?

Chris Shaw
Chris Shaw
26,676 Points

I think there is general confusion about what each function does, wp_register_style simply lets WordPress know that the style path should be known within the backend code but no yet queued for the page whereas wp_enqueue_style actually adds the style link to the page source.

In the nutshell.

wp_register_style

This allows you to name a custom style and it's path without queuing it for use on the page itself, when you register a style it's best practice to use wp_deregister_style to ensure that any plugins loading the same resource don't overwrite your style registration. Once the style is registered it sits in the backend waiting for wp_enqueue_style to be called for the style $handle.

wp_enqueue_style

This allows you to queue up a pre-registered style so that WordPress will output the link path to the page source, if you don't need the style to be loaded on a specific page you can use wp_dequeue_style which will remove the style $handle from the backend queue and prevent it from appearing in the page source.

Luke Travis
Luke Travis
2,887 Points

thanks for the wise words chris!