Add JSON as an alternative to PHP/CSS file headers for plugin and theme metadata#11200
Add JSON as an alternative to PHP/CSS file headers for plugin and theme metadata#11200retlehs wants to merge 2 commits intoWordPress:trunkfrom
Conversation
|
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 Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @QWp6t. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Hi @retlehs! 👋 Thank you for your contribution to WordPress! 💖 It looks like this is your first pull request to No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description. Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making. More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook. Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook. If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook. The Developer Hub also documents the various coding standards that are followed:
Thank you, |
693b1a7 to
84b82ea
Compare
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
|
||
| $extra_plugin_headers = (array) apply_filters( 'extra_plugin_headers', array() ); | ||
| foreach ( $extra_plugin_headers as $extra_header ) { | ||
| $key_map[ $extra_header ] = $extra_header; |
There was a problem hiding this comment.
This odd behavior that prevents custom keys stems from https://core.trac.wordpress.org/ticket/8964 — no valid reason was given.
Can we rectify it? It would allow us to quickly deprecate and assign alternative names without changing keys.
Trac ticket: https://core.trac.wordpress.org/ticket/24152
This adds support for declaring plugin and theme metadata in JSON files as an alternative to PHP/CSS comment-based file headers.
plugin.jsonfile alongside the main PHP filemetadataproperty oftheme.jsonMotivation
PHP/CSS file headers are parsed via regex on the first 8KB of a file. JSON provides a structured, machine-readable, language-agnostic alternative that:
style.css(block themes already don't need one for styling)See: WordPress File Headers Have Overstayed Their Welcome
Examples
plugin.json{ "name": "My Plugin", "uri": "https://example.com/my-plugin", "description": "A short description of the plugin.", "author": "Plugin Author", "authorUri": "https://example.com", "version": "1.0.0", "textDomain": "my-plugin", "requires": { "wordpress": "7.0", "php": "8.0", "plugins": ["woocommerce", "jetpack"] }, "network": true, "mainFile": "bootstrap.php" }theme.json(metadata property){ "version": 3, "metadata": { "name": "My Theme", "uri": "https://example.com/my-theme", "description": "A short description of the theme.", "author": "Theme Author", "authorUri": "https://example.com", "version": "1.0.0", "tags": ["blog", "one-column"], "textDomain": "my-theme", "template": "parent-theme" }, "settings": { ... }, "styles": { ... } }Other examples from my demo repo: https://github.com/retlehs/json-metadata-theme-and-plugin
Changes
Plugin metadata (
plugin.json)_get_plugin_json_data()function readsplugin.jsonfrom a plugin's directoryget_plugin_data()checks for JSON metadata first, then falls back to PHP file headersWP_PLUGIN_DIR(not mu-plugins or drop-ins)mainFileproperty to designate the main entry point (defaults to{dir-name}/{dir-name}.php)requires.pluginsarray maps to comma-separatedRequiresPluginsheader formatnetworkfield uses strict boolean checking (trueonly)wp_json_file_decode()) to avoid errors during enumerationextra_plugin_headers filterTheme metadata (
theme.json→metadata)read_json_metadata()private method onWP_Themereadsmetadatafromtheme.jsonstyle.cssheaderstheme.jsonmetadata can now work without astyle.cssfilevalidate_current_theme()to allow active themes (including child themes) withoutstyle.csswhentheme.jsoncontains validmetadata.nametheme.jsonextra_theme_headersfilterSchema notes
The
theme.jsonschema (maintained in the Gutenberg repo) will need a companion PR to add themetadataproperty definition.Schema publication/documentation for
plugin.jsonwill need to be done.Testing
npm run test:php -- --filter "Tests_Admin_IncludesPluginJsonMetadata|Tests_Admin_WPPluginDependencies_Initialize|Tests_Theme_ThemeDir|Tests_Theme_wpThemeJsonMetadata"All filtered tests passed locally in Docker
Demo
See https://github.com/retlehs/json-metadata-theme-and-plugin for an example plugin and theme, along with instructions on how to setup your local environment with this PR
Use of AI Tools
This PR was authored with Claude Code and Codex. Both were used for implementation, test writing, and code review. I reviewed and validated all changes, and ran tests locally in Docker.