-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Description
What problem does this address?
See https://core.trac.wordpress.org/ticket/56945 and WordPress/wordpress-develop#3536 (comment):
- Parsing
theme.jsonis a rather expensive process. Despite ongoing optimization efforts (e.g. https://core.trac.wordpress.org/ticket/56974), this can only get us so far. Especially for classic themes that do not even provide atheme.json, parsing WordPress core'stheme.jsonis an unnecessary overhead. - As discussed in Fix
wp_headperformance regression for classic themes wordpress-develop#3536, for classic themes it is still necessary to use some of the data from core'stheme.json, however given that no othertheme.jsonneeds to be parsed in that context, there would be a notable performance win (e.g. ~28% fasterwp_headexecution) from not even having to parse anytheme.jsonfile at runtime.
What is your proposed solution?
- Instead of parsing the built-in
theme.jsonduring runtime in PHP, it should be parsed into a ready-to-usetheme-json.phpfile as part of the Gutenberg / WP core build process. - The build process should render a PHP file returning an array with all the data from
theme.json, including the relevant strings (based ontheme-i18n.json) being wrapped in_x()calls. - This way, Gutenberg and WordPress core can simply
requirethat PHP file rather than having to run the expensive logic to parsetheme.jsonover and over again.- Potentially, to simplify working with
theme.jsonduring development, logic could be provided to still use the originaltheme.jsondecoding logic e.g. ifWP_DEBUGis enabled or something like that.
- Potentially, to simplify working with
This idea originally was proposed in WordPress/wordpress-develop#3536, but was then moved out of there so that the rest of the PR could be merged to make it into the WordPress 6.1.1 release. A previous version of the PR includes an example for that theme-json.php file (manually created using var_export(), please ignore the WPCS violations in there) - I'm linking this here just for reference, of course this file would only make sense to be generated automatically for a reasonable implementation, as proposed above.
Potentially, we could take this even further by making the build tool to create a PHP file from theme.json available to external theme developers as well. This way, theme developers could optionally adopt that tool and provide an already parsed theme-json.php for their own theme, so that the performance issue can be addressed more holistically.