Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use wp_enqueue_style() in my WordPress theme?

Tags:

I am building out my first WordPress site for a client. I really want to use LESS for CSS and found a WP plugin named WP-LESS.

Now, I am total WordPress newb, but it appears that this plugin requires me to use a function called wp_enqueue_style() to tell WordPress to process the .less file.

I can't figure out where I use this function. I looked in my header.php file in my theme directory and I see this.

<link rel="stylesheet" type="text/css" media="all"
  href="<?php bloginfo( 'stylesheet_url' ); ?>" />

Am I supposed to replace this code with something like this?

<?php wp_enqueue_style('mytheme',
  get_bloginfo('template_directory').'/style.less',
  array('blueprint'), '', 'screen, projection'); ?>
like image 350
jessegavin Avatar asked Aug 12 '10 21:08

jessegavin


People also ask

What is use of Wp_enqueue_style in WordPress?

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.”

How do I enqueue CSS files in WordPress?

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. wp_enqueue_style( $handle , $src , $deps , $ver , $media ); ?> The parameters are exactly the same with the wp_register_style() function, so there's no need to repeat them.

How do I add an external CSS file to WordPress?

To add external CSS and Javascript, first enqueue the script or style using wp_enqueue_script() or wp_enqueue_style(). You should load the style using wp_enqueue_style instead of loading the stylesheet in your header. php file.


2 Answers

Not quite, but almost. What you want to do is place a function in functions.php that queues your script.

So:

function addMyScript() {
    wp_enqueue_style('mytheme', get_bloginfo('template_directory').'/style.less', array('blueprint'), '', 'screen, projection');
}
add_action('wp_head', 'addMyScript');

Then make sure you have do_action('wp_head'); in your header.php file and it should work just fine.

like image 142
EAMann Avatar answered Dec 09 '22 06:12

EAMann


wp_enqueue_style usage inside of the theme or the plugin:

wp_enqueue_style( 'my-style', get_template_directory_uri() . '/css/my-style.css', false, '1.0', 'all' ); // Inside a parent theme
wp_enqueue_style( 'my-style', get_stylesheet_directory_uri() . '/css/my-style.css', false, '1.0', 'all' ); // Inside a child theme
wp_enqueue_style( 'my-style', plugins_url( '/css/my-style.css', __FILE__ ), false, '1.0', 'all' ); // Inside a plugin
like image 22
lime-cat Avatar answered Dec 09 '22 07:12

lime-cat