How to Conditionally Load Styles for Custom Templates in the WordPress Template Chooser
Content
Many WordPress theme developers face a common challenge: ensuring their custom template styles appear consistently across the frontend, the page editor, and the template chooser. A user recently reported that while their conditional styles worked perfectly on the frontend and in the editor, they failed to load in the template chooser interface. This guide explains why this happens and provides a solution.
Understanding the Problem
The core issue lies in how the template chooser operates. Unlike the frontend or page editor, which can rely on server-side PHP functions like get_page_template_slug(), the template chooser is a client-side JavaScript application. When you navigate through the template chooser, it dynamically renders previews without making a full page reload to the server. This means any PHP-based logic for conditionally enqueuing stylesheets is never executed during this process.
Why theme.json Alone Isn't the Answer
Many developers, in an effort to modernize their workflow, try to use theme.json to solve this. However, theme.json is designed for global styles and settings. It currently lacks a mechanism to target and apply styles to a single, specific custom template. Its strength is in defining design systems, not conditional, template-specific overrides.
The Recommended Solution: A Hybrid Approach
The most effective way to achieve style parity is to use a combination of traditional enqueuing and JavaScript. Here’s a step-by-step method:
- Identify Your Template with a Body Class: First, ensure your custom template adds a unique CSS class to the body tag. This is a classic theming technique that provides a consistent hook for your CSS.
- Enqueue for Frontend and Editor: Continue using your current method with
enqueue_block_editor_assetsand a check forget_page_template_slug()to load the styles on the frontend and in the main page editor. This works because those contexts involve server-side rendering. - Inject Styles for the Template Chooser with JavaScript: This is the key to solving the chooser problem. You need to write a small script that checks if the template chooser is active and if the preview for your specific template is being displayed. If so, it should inject your template's CSS directly into the page.
Example Code Snippet
The following is a conceptual example of how you might approach the JavaScript portion. You would enqueue this script specifically for the block editor.
// Enqueue your script for the block editor
function my_theme_enqueue_template_chooser_script() {
wp_enqueue_script(
'my-theme-template-chooser',
get_template_directory_uri() . '/js/template-chooser.js',
array( 'wp-blocks', 'wp-element', 'wp-data' )
);
}
add_action( 'enqueue_block_editor_assets', 'my_theme_enqueue_template_chooser_script' );
Then, in your template-chooser.js file, you would need to listen for changes in the editor state, detect when your template is selected in the chooser, and then dynamically add a <style> tag containing your specific CSS rules to the document head.
Conclusion
Achieving consistent styles for a custom template in all parts of the WordPress admin requires understanding the different rendering contexts. While server-side PHP is perfect for the frontend and editor, the client-side nature of the template chooser demands a JavaScript-based solution. By combining both methods, you can ensure your design looks perfect everywhere your client interacts with it.
This approach is based on community reports and analysis. For deeper technical implementation details, consulting the official Gutenberg repository for ongoing developments is always recommended.
Related Support Threads Support
-
Font Anton Regular added – all weights displayed under appearancehttps://wordpress.org/support/topic/font-anton-regular-and-medium-font-added-all-displayed-under-appearance/
-
What’s the Best Practice for Loading CSS for Each Template Part?https://wordpress.org/support/topic/whats-the-best-practice-for-loading-css-for-each-template-part/
-
Using theme.json to hide Typography Orientation block settinghttps://wordpress.org/support/topic/using-theme-json-to-hide-typography-orientation-block-setting/
-
Override global stylehttps://wordpress.org/support/topic/override-global-style/
-
Can’t disable child theme stylehttps://wordpress.org/support/topic/cant-disable-child-theme-style/
-
$wp_styles object initalize to another class?https://wordpress.org/support/topic/wp_styles-object-initalize-to-another-class/
-
What’s this new frontend CSS ouptut in 6.5?https://wordpress.org/support/topic/whats-this-new-frontend-css-ouptut-in-6-5/
-
Disable the default font sizes (S, M, L, ….) for blockshttps://wordpress.org/support/topic/disable-the-default-font-sizes-s-m-l-for-blocks/
-
editor-style.css changes ignoredhttps://wordpress.org/support/topic/editor-style-css-changes-ignored/
-
Applying Conditional Styles to a Custom Template Using theme.json in WordPresshttps://wordpress.org/support/topic/applying-conditional-styles-to-a-custom-template-using-theme-json-in-wordpress/
-
In 6.5 toggling FixedToolbar not workinghttps://wordpress.org/support/topic/in-6-5-toggling-fixedtoolbar-not-working/
-
Style Variations and Hybrid / Classic Themeshttps://wordpress.org/support/topic/style-variations-and-hybrid-classic-themes/
-
Show WYSIWYG editor Styles in dropdownhttps://wordpress.org/support/topic/show-wysiwyg-editor-styles-in-dropdown/
-
add_editor_style making Gutenburg uglyhttps://wordpress.org/support/topic/add_editor_style-making-gutenburg-ugly/
-
Using theme.json Presets in Style.css?https://wordpress.org/support/topic/using-theme-json-presets-in-style-css/
-
Using selective refresh in themehttps://wordpress.org/support/topic/using-selective-refresh-in-theme/
-
Preg replace in contenthttps://wordpress.org/support/topic/preg-replace-in-content/
-
Style Link in WYSIWYG editors … not working wellhttps://wordpress.org/support/topic/style-link-in-wysiwyg-editors-not-working-well/
-
Dynamic Theme Style Updatehttps://wordpress.org/support/topic/dynamic-theme-style-update/
-
Disable Automatic Overlay Color on Cover Blocks (WP 6.5)https://wordpress.org/support/topic/disable-automatic-overlay-color-on-cover-blocks-wp-6-5/
-
add_editor_style Does Not Support @media Querieshttps://wordpress.org/support/topic/add_editor_style-does-not-support-media-queries/
-
Switch style buttonhttps://wordpress.org/support/topic/switch-style-button/
-
Using Duotone outside singleshttps://wordpress.org/support/topic/using-duotone-outside-singles/
-
Add appearance tools to Classic themeshttps://wordpress.org/support/topic/add-appearance-tools-to-classic-themes/
-
google fonts loadinghttps://wordpress.org/support/topic/google-fonts-loading-3/
-
Custom WordPress Styles Variations for Block Themes using Pluginhttps://wordpress.org/support/topic/custom-wordpress-styles-variations-for-block-themes-using-plugin/
-
Add color reference to css that alters with a new theme….https://wordpress.org/support/topic/add-color-reference-to-css-that-alters-with-a-new-theme/
-
Printing theme color presets in adminhttps://wordpress.org/support/topic/printing-theme-color-presets-in-admin/
-
No style for “ Card ” dark mode articleshttps://wordpress.org/support/topic/no-style-for-card-dark-mode-articles/
-
load_theme_textdomain not working anymore WP 6.8 ?https://wordpress.org/support/topic/load_theme_textdomain-not-working-anymore-wp-6-8/