Repository navigation
Add plugin template registration API - #61577
Conversation
|
Size Change: +45 B (0%) Total Size: 1.77 MB
ℹ️ View Unchanged
|
f3d4e5a to
03c90ff
Compare
2fa0a91 to
1390923
Compare
|
This pull request has changed or added PHP files. Please confirm whether these changes need to be synced to WordPress Core, and therefore featured in the next release of WordPress. If so, it is recommended to create a new Trac ticket and submit a pull request to the WordPress Core Github repository soon after this pull request is merged. If you're unsure, you can always ask for help in the #core-editor channel in WordPress Slack. Thank you! ❤️ View changed files❔ lib/block-templates.php ❔ lib/class-wp-block-templates-registry.php ❔ lib/compat/wordpress-6.7/class-gutenberg-rest-templates-controller-6-7.php ❔ lib/compat/wordpress-6.7/compat.php ❔ lib/compat/wordpress-6.7/rest-api.php ❔ lib/load.php |
da50079 to
b244dc9
Compare
|
Flaky tests detected in ae59c2c. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/9682409402
|
1c13545 to
54d2199
Compare
|
@youknowriad @ntsekouras this PR is still missing tests and documentation, but feature-wise everything should be working, so I would really appreciate it if you could take a look and tell me whether this approach looks good or not. 🙏 In #41401, there were some proposals of next steps, but for now I'm keeping the PR small, only introducing the basic API. This way we can iterate on it in the future. |
|
Looking at the shape of the API. The first thing that comes to my mind is whether we should keep the "path" argument or replace it with a "content" argument instead that could be more flexible? I didn't give it too much thoughts though, and would appreciate opinions. |
|
It would be great to get a lot of eyes on this PR, specially from folks familiar with Core and PHP @WordPress/gutenberg-core |
|
I would love if we can think of a declarative way to actually absorb the |
|
Thanks for taking a look, @youknowriad!
Good point! I initially started with the
You mean doing it as part of the template registration API or as a follow-up? I'm a bit wary of having a big scope for the first version of the API. Even though in the testing steps I added a |
|
I guess it's fine as a follow-up, so you're saying that by default these registered templates will serve as "custom templates" you can assign to posts/pages... right? |
|
Is this meant to be a GB only experimental API for now? |
|
@ellatrix For me at least, this shouldn't land in 6.6, we need a bit of time to iron things out properly. |
Yes, exactly. With the example code defining |
|
I am just curious, why is it named We have other APIs with similar naming, such as: I think |
Since WP do not use Namespaces shouldn't all functions be prefixed with |
|
I think the |
|
I agree it feels weird that the |
|
I agree it's best to be consistent. I think we have time to drop the prefix. @youknowriad It's possible I suggested it (can't remember but it's the sort of thing I'd do 😄) but that would have been ignorance of convention if I did so. It would be nice if they all had the wp_ prefix but the opportunity has been missed. |
|
Possibly due to this documentation Specifically
Do we need to update that documentation or add qualifiers/exceptions to the rules? |
|
Is it possible to register a |
It's very hard to come up with a good rule. But yeah for this case (registration functions) it seems better to avoid the prefix.
We want to ultimately absorb template parts in patterns and patterns already have registration API. Can anyone help with the renaming work? |
|
Thanks for the feedback, folks!
I opened a PR in Gutenberg (#65958) and WP core (WordPress/wordpress-develop#7543). I'm not sure how backwards compatibility needs to be handled in this case, considering this wasn't released in WP core. I assume we don't need to worry about backwards compatibility in WP, but what about Gutenberg, is it safe to simply rename the function, or should we keep the old names and mark them as deprecated? |
Keeping the old names and deprecating in Gutenberg (but simply renaming in core) sounds like a good approach to me! Here's an example of another place in Gutenberg where there are deprecated functions, in case it helps: gutenberg/lib/experimental/script-modules.php Lines 192 to 202 in 57e4be7 |
|
Thanks, @andrewserong! I updated #65958 accordingly. 🙂 |
@ajgagnon I created a separate issue for this here: #65861 The core has the It might be possible to use the new
@youknowriad Do you mean that template parts will become patterns in the future? |
For me, yes, Read this following link where I detail my thinking. |
Agreed with dropping the |
|
Does |
|
@helgatheviking See answer above #61577 (comment) |
…des for plugin-registered templates This commit introduces a new API to allow plugins to easily register block templates with `wp_register_block_template()` and the `WP_Block_Templates_Registry` class, addressing the complexity of hooking into multiple filters. It also ensures plugin-registered templates overridden by themes fall back to the plugin-provided title and description when the theme doesn't define them. See WordPress/gutenberg#61577. See WordPress/gutenberg#64610. Fixes #61804. Props aljullu, peterwilsoncc, antonvlasenko, azaozz, youknowriad, noisysocks. git-svn-id: https://develop.svn.wordpress.org/trunk@59073 602fd350-edb4-49c9-b593-d223f7449a82

What?
This PR introduces a new API so plugins can easily register templates.
Closes #41401.
Backport PR in core: WordPress/wordpress-develop#7125.
Why?
Currently, it's not easy for plugins to register templates, as they need to hook into several filters.
How?
This PR creates a new function:
wp_register_block_template()as well as aWP_Block_Templates_Registryclass. They are based onregister_block_type()andWP_Block_Type_Registry.You can read more about the API shape in this comment: #41401 (comment).
Testing Instructions
Basic template customization:
/category/test-category/) and verify the Plugin Template contents are rendered.Plugin Templateappears with the correct title and description.With theme customization:
/templates/plugin-template.htmltemplate to your active theme and repeat steps 2-5./templates/plugin-template.htmlfrom your theme before proceeding with the next steps.Using the template in a post type:
Deactivating the plugin:
Unregistering templates:
Registering WP default templates:
/category/test-category/).category.htmltemplate to your theme.Registering templates already customized by the user:
/author/admin/).Devnote