How to Enqueue CSS and JS in Wordpress Block Themes (and load it only on CERTAIN templates!)
How to Enqueue CSS and JS in Wordpress Block Themes (and load it only on CERTAIN templates!)
Friday - June 17, 2022
Block themes are a very promising direction the Wordpress project is taking. I love being able to avoid PHP as much as possible to design websites and with block themes you are essentially using static HTML.
One problem I ran into was enqueing CSS or JS files ONLY on certain templates. While with most websites this is no big deal, but if you are doing something like creating highly optimized landing pages that you want zero unused CSS or JS in, this becomes more important.
How To Enqueue CSS in Wordpress Block Themes
It’s no different than with regular Wordpress themes.
You have to add a snippet of code similar to this in your theme’s functions.php.
How To Enqueue CSS in Wordpress Block Themes Only on Specific Template
Credit
Enqueing a stylesheet/script on a specific template page was one of those problems that stumped me for days. The normal Wordpress template related functions just did not work. Big, big thanks to S. Walsh who answered my StackOverflow question.
Leave a comment