Make WordPress Core

Changeset 60688


Ignore:
Timestamp:
08/28/2025 07:08:21 PM (7 months ago)
Author:
Bernhard Reiter
Message:

Block Hooks: Apply block hooks to plugin-registered templates.

As of WordPress 6.7.0, it is possible -- e.g. for plugins -- to register block templates via a new API, register_block_template(). Unlike block templates loaded from theme files or from the database, however, those block templates didn't have Block Hooks applied. This changeset rectifies this inconsistency.

Props iamadisingh, aljullu, bernhard-reiter.
Fixes #63808.

File:
1 edited

Legend:

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

    r60339 r60688  
    12281228                }
    12291229            );
    1230             $query_result                  = array_merge( $query_result, $matching_registered_templates );
     1230
     1231            $matching_registered_templates = array_map(
     1232                function ( $template ) {
     1233                    $template->content = apply_block_hooks_to_content(
     1234                        $template->content,
     1235                        $template,
     1236                        'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
     1237                    );
     1238                    return $template;
     1239                },
     1240                $matching_registered_templates
     1241            );
     1242
     1243            $query_result = array_merge( $query_result, $matching_registered_templates );
    12311244        }
    12321245    }
     
    13241337
    13251338/**
    1326  * Retrieves a unified template object based on a theme file.
     1339 * Retrieves a unified template object based on a theme file or plugin registration.
    13271340 *
    13281341 * This is a fallback of get_block_template(), used when no templates are found in the database.
     1342 * Also checks for templates registered via the Template Registration API.
    13291343 *
    13301344 * @since 5.9.0
     
    13711385
    13721386    $block_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $slug );
     1387
     1388    if ( $block_template ) {
     1389        $block_template->content = apply_block_hooks_to_content(
     1390            $block_template->content,
     1391            $block_template,
     1392            'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
     1393        );
     1394    }
    13731395
    13741396    /**
Note: See TracChangeset for help on using the changeset viewer.