wp_enque_style

How to use wp_enqueue_style in WordPress template.

by Expert of Tech

In this article, we will discuss how to properly use wp_enqueue_style in the latest version of WordPress template files.

Many WordPress plugin and theme developers use wp_enqueue_style function to register stylesheets with wp_head hook. Once you know how to wp_enqueue_style, it is quite easy to do in your theme or in plugins.

WordPress loads stylesheets in wp-content/themes/themeName/style.css (or wp-includes/css if style.css doesn’t exist). So you should always include all the stylesheets inside this file for your theme.

But wp_enqueue_style will load stylesheets stored outside of wp-content directory into <head> section like wp-content/themes/themeName/header.php wp_enqueue_style wp-content/themes/themeName/css/custom.css

So, wp_enqueue_style will work only if the stylesheet is inside wp-content directory or inside wp-includes directory. For other cases you should use wp_register_style function.

For all the wp_enqueue_style examples you can find at codex: wp_enqueue_style($handle, $src, $deps);

Main parameter:

$handle – pointer to registered style handle in wprockets (you will need this only if you don’t pass it as argument)

$src – path to stylesheet file

$deps – array of dependencies listed here: http://codex.wordpress.org/Function_Reference/wp_enqueue_style

Note: wp_enqueue_script, wp_register_script and wp_print_scripts have almost the same parameters as wp_enqueue style except src should be a path to javascript file instead of a CSS stylesheet.

<!– wprocket example –> <link rel=”stylesheet” href=”<?php wproclieStyle(“myHandle”) ?>/” type=”text/css” media=”screen”> <!– end wprocket example –>

wp_enque_style with wp rocket plugin

wp_register_style(“myStyle”, $handle); // $handle – pointer to registered handle in wprockets wprocketsRegisterStyle(“myHandle”, $filePath); // $filePath – absolute path of your file wpHeaderCss($handle,$srcFile){ wproclieRegisterStyle(“myHandle”, getTemplateDir().’/myFile.php’); wpHeaderCss(“myHandle”); wpFooterCss(“myHandle”); }

You can do a lot more with wprocket like creating and using your own wprockets. You should check wproclie docs for that.

If you want your plugin or theme to use only specific handle (i.e. avoid using all registered handles), do this:

<?php wp_deregister_style(‘myHandle’); wp_enqueue_style(“myHandle”, …); wp_register_style(“myHandle”, …); ?>

You can check wprockets registered handles list by this curl command: curl wprocketList

If you want your plugin or theme to use wpDashboard stylesheet, wpHeader and wpFooter, do this if (!function_exists(‘wpDashboard’)) { function wpDashboard(){ wpb(); } } If you need only one of them and don’t want to include whole wproclie class for it, do this: <!– wpcss example –> <link rel=”stylesheet” id=”dashboard-css” href=”/<?php wproclieDashboard(“dashboardCss”); ?>” type=”text/css”/> <!– end wpcss example –>

Learn more about wp_enque_style

You can check wpDashboards registered stylesheets list by this curl command: curl wpdashboardList

Note that wp_enqueue_style function has priority over wp_register_style since it is hooked to wpHead hook. So you should always use wp_enqueue_style in your themes. And if you want to add new handle in future, use wpRegisterStyle instead of writing whole process again. You can write if(!wpRegisterStyle) inside wpHeader file so scripts will work for old wp versions. wpshout has an example of this here: wpinglor

In some cases you need to pass all stylesheets dependencies as url() parameter to wp_enqueue_style function. In wproclie I had a problem that wpsce-css.php is loaded first and then wpcss.php so if i had a dependency on wpsce-css, there was a big chance that it will be loaded before my css file and thus my style will have no effect since wpScout uses same handle(themeName/wpScout) as default css file if none is defined in theme settings. You can avoid using @import instead of the wp_enqueue_style function like wpsce-css does. wpsce-css is a wproclie wprocket and uses wpRegisterStyle instead of wp_enqueue_style to avoid wpsce css file loading first and then mine: wpshout

<?php wpcss(); ?> <!– end wpcss –>

<?php wprcLink();?> <!– end wprcLink –>

If we don’t want our plugin or theme stylesheets linked to the theme, we can remove them from the header as follows: wpmalboro

if ( !is_admin() && apply_filters(‘woocommerce_template’,get_template_directory_uri().’/woocommerce/myStyle.css’) ) wp_register_style(‘themeName’,’ url(“<?php wproclieStyle(“myHandle”); ?>”) ‘); wpprint (” <?php wpHeaderCss(‘themename’); wprcLink(); wpFooterCss(‘themename’); wpshout() ?> “)

ARTICLE END.

More posts you might be interested in: wp_enqueue_scripts wprocket wpsce wpshout wpcss wpdashboard wpregisterstyle wpheadercss wptheme wpthemestyle wpprint

Please enable JavaScript to view the comments powered by Disqus.

In conclusion, That’s all about it. But, if you face any difficulties, feel free to contact one of our experts or email me. I will respond ASAP. To learn more about WordPress speed optimization, click here.