[ad_1]
Since the introduction of block Block is the abstract term used to describe units of markup that, composed together, form the content or layout of a webpage using the WordPress editor. The idea combines concepts of what in the past may have achieved with shortcodes, custom HTML, and embed discovery into a single consistent API and user experience. themes, theme authors have been able to create custom block templates. However, this possibility was limited to themes, leaving plugins without a straightforward way to register their own templates. To achieve similar functionality, plugins had to resort to complex methods, including hooking into multiple PHP The web scripting language in which WordPress is primarily architected. WordPress requires PHP 5.6.20 or higher filters, manually creating WP_Block_Template
objects, and replicating internal WordPress logic.
WordPress 6.7 introduces a new API An API or Application Programming Interface is a software intermediary that allows programs to interact with each other and share data in limited, clearly defined ways. that will significantly simplify the process for plugins to register their own block templates. To enable this, two new functions have been introduced: register_block_template()
for registering a block template and unregister_block_template()
for unregistering it.
register_block_template( string $template_name, $args = array() )
accepts two parameters, that define how the template is registered:
$template_name
: The name of the template in the form of plugin_uri//template_name (note that this requires a double //).$args
: An array of arguments for defining the template:title
: An internationalized title for the template.description
: An internationalized description of the template.content
: The default content (block markup) for the template when rendered in the editor or on the front end.post_types
: An array of post type slugs to make available to users as per-post custom templates.
For example, a plugin A plugin is a piece of software containing a group of functions that can be added to a WordPress website. They can extend functionality or add new features to your WordPress websites. WordPress plugins are written in the PHP programming language and integrate seamlessly with WordPress. These can be free in the WordPress.org Plugin Directory https://wordpress.org/plugins/ or can be cost-based plugin from a third-party can register a template with the following snippet:
register_block_template( 'devblog-plugin-templates//my-template', [
'title' => __( 'Example', 'devblog-plugin-templates' ),
'description' => __( 'An example block template from a plugin.', 'devblog-plugin-templates' ),
'content' => '
This is a plugin-registered template.
'
] );
Once registered, the template will appear alongside all other templates in the Site Editor:
And, if needed, could later unregister it with:
unregister_block_template( 'devblog-plugin-templates//my-template' )
Plugins can also override templates from the WordPress template hierarchy, such as the archive page of a custom post type WordPress can hold and display many different types of content. A single item of such a content is generally called a post, although post is also a specific post type. Custom Post Types gives your site the ability to have templated posts, to simplify the concept. or a specific author page.
Theme templates take priority over plugin-registered templates, allowing themes to override specific plugin templates just as they can with WordPress templates.
Currently, this API is limited to block templates and does not allow the registration of block template parts.
You can read more about this new feature in the following links:
Props to @aljullu for writing this dev note Each important change in WordPress Core is documented in a developers note, (usually called dev note). Good dev notes generally include a description of the change, the decision that led to this change, and a description of how developers are supposed to work with that change. Dev notes are published on Make/Core blog during the beta phase of WordPress release cycle. Publishing dev notes is particularly important when plugin/theme authors and WordPress developers need to be aware of those changes.In general, all dev notes are compiled into a Field Guide at the beginning of the release candidate phase.. And thanks to @fabiankaegy for reviewing
[ad_2]
Read more