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

Add support for adding templates and template parts for plugins #42362

Closed
costasovo opened this issue Jul 12, 2022 · 4 comments
Closed

Add support for adding templates and template parts for plugins #42362

costasovo opened this issue Jul 12, 2022 · 4 comments
Labels
[Feature] Extensibility The ability to extend blocks or the editing experience [Feature] Site Editor Related to the overarching Site Editor (formerly "full site editing") [Feature] Templates API Related to API powering block template functionality in the Site Editor Needs Technical Feedback Needs testing from a developer perspective. [Type] Enhancement A suggestion for improvement.

Comments

@costasovo
Copy link
Contributor

What problem does this address?

Currently, templates are defined within the theme (source theme) and can be edited in the site editor (source custom). There are some use cases where plugins could also use the possibility to add a template. WooCommerce blocks plugin already adds templates for Product Catalog page, Product Page, etc. At MailPoet, we are considering using templates and templates parts for email templates.
WooCommerce Blocks developers documented how they added the templates. As you can see from the docs, adding a template is currently cumbersome.

What is your proposed solution?

This could be solved by adding an API for registering a template. I don't know the internals in detail, so I struggle with proposing the solution.

I was thinking about a function like:

/**
 * @param WP_Block_Template
 */
function register_block_template( WP_Block_Template $template ) {}

or maybe just

function register_block_template( array $args ) {}

The $args would cover parameters for defining the block template.

It could be used for both templates and template_parts.

@ndiego ndiego added the [Type] Enhancement A suggestion for improvement. label Jul 12, 2022
@kathrynwp kathrynwp added [Feature] Full Site Editing [Feature] Templates API Related to API powering block template functionality in the Site Editor Needs Technical Feedback Needs testing from a developer perspective. labels Jul 12, 2022
@gigitux
Copy link
Contributor

gigitux commented Jun 29, 2023

It isn't easy for a plugin to register a custom block template. I created a POC (I used @wordpress/create-block for the boilerplate), to show it: https://github.com/gigitux/poc-register-template-plugin/blob/main/poc-register-template-plugin.php.

Also, this is a production class that handles Block Templates for WooCommerce Blocks:

https://github.com/woocommerce/woocommerce-blocks/blob/2b79d3e87dffd5b38d5910ebcf39057e267d190d/src/BlockTemplatesController.php

As you can see, it is complex registering and handles custom block templates for a plugin.

Also, the current POC is bugged because it doesn't load the custom block template from the database after the user saves a new version of the template.

Following my exploration, it is necessary using three filters to register a block template:

  • pre_get_block_template
  • pre_get_block_file_template
  • get_block_templates

Also, it is necessary to write custom logic to read the directory where the templates are placed and parse the filename to get the template's name.

It would be great to have a better way/specific API to register block templates.

@annezazu annezazu added [Feature] Site Editor Related to the overarching Site Editor (formerly "full site editing") and removed [Feature] Full Site Editing labels Jul 24, 2023
@gziolo gziolo added the [Feature] Extensibility The ability to extend blocks or the editing experience label Aug 7, 2023
@fabiankaegy
Copy link
Member

I think this ticket may be a duplicate of #41401

I'm going to leave it open for now since it already has some valuable context here but want to make both sides aware of one another :)

@Aljullu
Copy link
Contributor

Aljullu commented Apr 30, 2024

Just a heads-up that I left a comment with an API proposal in #41401 (comment).

I think this ticket may be a duplicate of #41401

+1 to closing this issue as duplicate of #41401, given that the other one is older.

@fabiankaegy
Copy link
Member

Closing to keep #41401 as the main source of truth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Extensibility The ability to extend blocks or the editing experience [Feature] Site Editor Related to the overarching Site Editor (formerly "full site editing") [Feature] Templates API Related to API powering block template functionality in the Site Editor Needs Technical Feedback Needs testing from a developer perspective. [Type] Enhancement A suggestion for improvement.
Projects
Development

No branches or pull requests

8 participants