Make WordPress Core


Ignore:
Timestamp:
09/20/2024 02:05:50 AM (9 months ago)
Author:
noisysocks
Message:

Editor: Add plugin template registration API and improve theme overrides 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 https://github.com/WordPress/gutenberg/pull/61577.
See https://github.com/WordPress/gutenberg/pull/64610.

Fixes #61804.
Props aljullu, peterwilsoncc, antonvlasenko, azaozz, youknowriad, noisysocks.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-template.php

    r57785 r59073  
    359359    }
    360360}
     361
     362/**
     363 * Register a block template.
     364 *
     365 * @since 6.7.0
     366 *
     367 * @param string       $template_name  Template name in the form of `plugin_uri//template_name`.
     368 * @param array|string $args           {
     369 *     @type string        $title                 Optional. Title of the template as it will be shown in the Site Editor
     370 *                                                and other UI elements.
     371 *     @type string        $description           Optional. Description of the template as it will be shown in the Site
     372 *                                                Editor.
     373 *     @type string        $content               Optional. Default content of the template that will be used when the
     374 *                                                template is rendered or edited in the editor.
     375 *     @type string[]      $post_types            Optional. Array of post types to which the template should be available.
     376 *     @type string        $plugin                Optional. Slug of the plugin that registers the template.
     377 * }
     378 * @return WP_Block_Template|WP_Error The registered template object on success, WP_Error object on failure.
     379 */
     380function wp_register_block_template( $template_name, $args = array() ) {
     381    return WP_Block_Templates_Registry::get_instance()->register( $template_name, $args );
     382}
     383
     384/**
     385 * Unregister a block template.
     386 *
     387 * @since 6.7.0
     388 *
     389 * @param string $template_name Template name in the form of `plugin_uri//template_name`.
     390 * @return WP_Block_Template|WP_Error The unregistered template object on success, WP_Error object on failure or if the
     391 *                                    template doesn't exist.
     392 */
     393function wp_unregister_block_template( $template_name ) {
     394    return WP_Block_Templates_Registry::get_instance()->unregister( $template_name );
     395}
Note: See TracChangeset for help on using the changeset viewer.