Conditionally Include Custom Styles/Stylesheets for Templates and Template Parts (akin to wp_enqueue_block_style()) #44060
Labels
CSS Styling
Related to editor and front end styles, CSS-specific issues.
[Feature] Templates API
Related to API powering block template functionality in the Site Editor
[Feature] Themes
Questions or issues with incorporating or styling blocks in a theme.
[Type] Enhancement
A suggestion for improvement.
What problem does this address?
When developing block themes, I've found myself wishing I could add styles for templates and template parts akin to how we
wp_enqueue_block_style()
. With the block editor including only necessary CSS for performance, this pattern was one I want to be available for templates and template-parts.For an example from a real-world project, Wikimedia Enterprise, an FSE website I constructed:
On the "Docs" page template, the design required a "sticky" sidebar. This is not a pattern available to the editor, but I was able to largely construct the layout without the "sticky" CSS. However, I needed custom CSS added to make the "sticky".
I could have of course added it to style.css, but with a goal of style.css one day being only necessary to define the theme, I wanted to find an alternative, not only to force myself into the design pattern but also to enjoy the benefits only loading the CSS when needed (and not on pages that don't need it).
My solution was to conditionally enqueue the CSS when
is_page()
andis_page_template('docs')
. This can and should be easier, especially in the day and age of theme.json.I also feel like like this level of customization is similar in spirit to @carolinan's suggestion on #43347.
What is your proposed solution?
In addition to @carolinan's suggestion...
wp_enqueue_block_style()
for each template-parts and templates (perhapswp_enqueue_template_style()
andwp_enqueue_template_part_style()
) that will conditionally add CSS specific for a template, and only when the template is included in render.templateParts
andcustomTemplates
array args oftheme.json
that will enable developers to include CSS conditionally when a template is included, either as a separate file, or as text, for example:and
The text was updated successfully, but these errors were encountered: