I read that I should insert my style.css file in my theme through the wp_enqueue_style() function because it's not a good solution to add that file through a link tag in the html of the header.php file.

Could you explain me the reasons?


Three good reasons to use the enqueue functions over hard coding are to eliminate duplication, avoiding library conflicts, and handling dependencies:

  • Duplication: If, for example, you hard code a link to jQuery, and then install a plugin which also enqueues the jQuery library, you're site will be loading the same library twice, which will affect the page load speed, amongst other things.
  • Library conflicts: Much like duplication, but the plugin may be loading a different version of jQuery, which results in your page trying to load two conflicting versions, which could cause serious page problems.
  • Handling dependencies: If you create a custom script which relies on a javascript library, you can add the dependency as a parameter of the enqueue function, which will ensure that the required library is loaded before the custom script. Easy enough to do with hard coding, but easier to keep track of with wp_enqueue_style().

Further reading here: https://www.seedprod.com/enqueuing-styles-scripts-wordpress/

  • I know that this is very old, but doesn't the nature of CSS being cascading negate these issues? I can definitely agree with you for javascript/jquery/libraries, but if it's just CSS? – little tiny man Mar 25 '20 at 22:54

If you hardcode the styles via <link> elements in your theme, plugins will not be able to act upon them. Some examples of the kinds of things that plugins can do with CSS files if you use the enqueue functions:

  • External caching (such as via Amazon S3, MaxCDN, Akamai, etc)
  • Concatenation, to reduce the number of browser requests.
  • Preprocessing on-the-fly (think SASS/LESS, for example)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.