I’m developing a new WordPress theme and I’m at the point where I’m adding theme customizer options. Most of them have been surprisingly easy to implement, but I’m stumped on a couple of important options:
- Switching between a light and dark theme
- Switching between two web font choices
I understand how to properly enqueue stylesheets and scripts and I’m getting pretty familiar with how to add settings, sections and controls to the Theme Customizer. What I need now is a way to conditionally load stylesheets (the light/dark skins) and scripts (the web fonts).
How can I do something like this?
// if the skin is set to 'light'
// enqueue light.css
// if the skin is set to 'dark'
// enqueue dark.css
or
// if the font style is set to 'sans-serif'
// enqueue source-sans-pro;
// if the font-style is set to 'serif'
// enqueue merriweather;
Got it! Here’s the code that worked for me.
— Theme Customizer settings and controls (mine are in a separate
customizer.php
file):— Enqueue the light/dark stylesheets in
functions.php
along with your other scripts/styles:Thanks to cale_b for pointing me in the right direction.
Try this…
Please make sure that you add your theme ability to delete the variable $light when it is not selected, or your theme will only register the light theme.
You can also create two functions and hooks to include the stylesheets and put the action hook inside the ‘if else’ statement like this: