Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global Styles: Add support for loading CSS from external files in theme.json #69564

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from

Conversation

im3dabasia
Copy link
Contributor

@im3dabasia im3dabasia commented Mar 13, 2025

What?

Closes: #69484

This PR adds a new feature to theme.json that allows theme authors to load CSS for blocks from external files instead of embedding it directly in the theme.json file. This is done through a new file: prefix syntax in the css property.

Why?

This enhancement allows theme authors to organize their CSS in dedicated files, and use it in the theme.json

How?

The implementation adds support for a special file: prefix in the css property value in theme.json. When detected, the code:

  1. Extracts the relative file path
  2. Resolves it to an absolute path within the theme directory
  3. Uses WordPress's built-in wp_enqueue_block_style() function to properly enqueue the file
  4. Applies the same approach for both block-level CSS and block style variations

This keeps the implementation simple while leveraging existing WordPress functions for style enqueueing, ensuring compatibility with the WordPress CSS loading system.

Testing Instructions

  1. Create a new theme or use an existing theme with a theme.json file I used test/emptytheme
  2. Activate the emptytheme in Appearance > Themes > Empty Theme
  3. Add the following to your theme's theme.json file:
"styles": {
  "blocks": {
    "core/navigation": {
      "css": "file:./assets/css/navigation.css"
    },
    "core/paragraph": {
      "css": "file:./assets/css/paragraph.css"
    }
  }
}
  1. Create the CSS files /assets/css/navigation.css and /assets/css/paragraph.css
  • /assets/css/navigation.css
.wp-block-navigation {
  border: 5px solid #d41ad1;
  background-color: #a7d5ee;
}
  • /assets/css/paragraph.css
.wp-block-paragraph {
  background-color: rgb(183, 21, 118);
}
  1. Create a page with navigation and paragraph blocks
  2. View the page and verify the CSS is applied correctly

Screenshots or screencast

A comprehensive video of how I tested this feature locally:

Screen.Recording.2025-03-14.at.4.29.29.PM.mov

@im3dabasia im3dabasia force-pushed the try/add-css-via-file-theme.json branch from cf039e9 to 07168cc Compare March 14, 2025 11:19
@im3dabasia im3dabasia marked this pull request as ready for review March 14, 2025 12:07
Copy link

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @EldarAgalarov.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: EldarAgalarov.

Co-authored-by: im3dabasia <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link

Warning: Type of PR label mismatch

To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.

  • Type-related labels to choose from: [Type] Automated Testing, [Type] Breaking Change, [Type] Bug, [Type] Build Tooling, [Type] Code Quality, [Type] Copy, [Type] Developer Documentation, [Type] Enhancement, [Type] Experimental, [Type] Feature, [Type] New API, [Type] Task, [Type] Technical Prototype, [Type] Performance, [Type] Project Management, [Type] Regression, [Type] Security, [Type] WP Core Ticket, Backport from WordPress Core, Gutenberg Plugin.
  • Labels found: .

Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task.

@shail-mehta shail-mehta added the [Type] Enhancement A suggestion for improvement. label Mar 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

theme.json: Load CSS style from file using wp_enqueue_block_style under the hood
2 participants