﻿<head><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4357510538660394"
     crossorigin="anonymous"></script></head>{"id":6529,"date":"2022-05-11T07:40:57","date_gmt":"2022-05-11T07:40:57","guid":{"rendered":"https:\/\/stackdiary.com\/?p=6529"},"modified":"2024-04-06T08:46:35","modified_gmt":"2024-04-06T08:46:35","slug":"wp-enqueue-style","status":"publish","type":"post","link":"https:\/\/stackdiary.com\/wp-enqueue-style\/","title":{"rendered":"How to Enqueue a CSS Stylesheet in WordPress"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">If you&#8217;re anything like me &#8211; you love to throw together various <a href=\"https:\/\/stackdiary.com\/useful-css-tricks\/\">CSS snippets<\/a> and use them to customize your WordPress site&#8217;s design. But, despite all the fun, those snippets can quickly add up to 1,000+ lines of CSS &#8211; which gets loaded on every single page, even if that custom CSS isn&#8217;t needed for that particular page; a big no-no for performance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I know for a fact that WP Rocket&#8217;s caching plugin has a feature that removes unused CSS, but it&#8217;s still in beta and might break things. Besides, by enqueuing the stylesheet &#8211; you can still reap the benefits of both minifying the file but also caching it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, the first thing you want to do is move all your custom CSS to a new stylesheet, in my case &#8211; I created a file called <strong>cstm-design.css<\/strong> and saved it in my root directory. You can change the directory to whatever you like, including \/wp-content\/ or your theme&#8217;s directory.<\/p>\n\n\n\n<p class=\"js-html-2 wp-block-paragraph\">As a side note, I did not move <em>all<\/em> my CSS to this file. Some classes I still want to style on a frequent basis, and having to edit the .css file and then reload the cache is too time-consuming of a process. So, only put the CSS inside your stylesheet file that you know you won&#8217;t need to edit on a frequent basis.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The next thing you want to do is open up your <strong>functions.php<\/strong> file &#8211; either from the Theme Editor panel or by doing it from the command line if you host your site on a VPS.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then copy &amp; paste this snippet:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\" data-line=\"\">add_action(&#039;init&#039;, &#039;my_custom_css_stylesheet&#039;);\n \nfunction my_custom_css_stylesheet() {\n    wp_register_style( &#039;custom-design&#039;, &#039;\/cstm-design.css&#039; );\n}\nwp_enqueue_style( &#039;custom-design&#039; );<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That&#8217;s it. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can change the file location by renaming the<strong> \/cstm-design.css<\/strong> part. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, if your custom stylesheet is inside <code class=\"\" data-line=\"\">\/wp-content\/<\/code> &#8211; you would need to change it to <strong>\/wp-content\/cstm-design.css<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It goes without saying this is a pretty <em>quick and dirty<\/em> technique. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you read the official reference, it&#8217;s possible to use <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_enqueue_style\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">wp_enqueue_style()<\/a> to add version numbers, call child themes, and also specify media types. In our method, we simply call it as is, and most caching plugins will rotate the version automatically.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re anything like me &#8211; you love to throw together various CSS snippets and use them to customize your&#8230;<\/p>\n","protected":false},"author":1,"featured_media":6532,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[346],"tags":[],"class_list":["post-6529","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress"],"wps_subtitle":"The right way to add your styles without breaking your site.","_links":{"self":[{"href":"https:\/\/stackdiary.com\/wp-json\/wp\/v2\/posts\/6529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/stackdiary.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stackdiary.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stackdiary.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/stackdiary.com\/wp-json\/wp\/v2\/comments?post=6529"}],"version-history":[{"count":2,"href":"https:\/\/stackdiary.com\/wp-json\/wp\/v2\/posts\/6529\/revisions"}],"predecessor-version":[{"id":78695,"href":"https:\/\/stackdiary.com\/wp-json\/wp\/v2\/posts\/6529\/revisions\/78695"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/stackdiary.com\/wp-json\/wp\/v2\/media\/6532"}],"wp:attachment":[{"href":"https:\/\/stackdiary.com\/wp-json\/wp\/v2\/media?parent=6529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stackdiary.com\/wp-json\/wp\/v2\/categories?post=6529"},{"taxonomy":"post_ta