Make WordPress Core

Changeset 61661


Ignore:
Timestamp:
02/17/2026 01:20:36 PM (6 weeks ago)
Author:
wildworks
Message:

Block Supports: Add autoRegister support for PHP-only block registration.

Introduces block support for PHP-only block registration, enabling developers to register blocks in PHP without requiring JavaScript registration code.

When a block declares 'supports' => array( 'autoRegister' => true ) along with a render callback, it is exposed to the client-side via a JavaScript global variable and registered automatically.

Example usage:

register_block_type(
	'my-plugin/example',
	array(
		'title'           => 'My Example Block',
		'attributes'      => array(
			'title' => array(
				'type'    => 'string',
				'default' => 'Hello World',
			),
			'count' => array(
				'type'    => 'integer',
				'default' => 5,
			),
		),
		'render_callback' => function ( $attributes ) {
			return sprintf(
				'<div %1$s>%2$s: %3$d items</div>',
				get_block_wrapper_attributes(),
				esc_html( $attributes['title'] ),
				$attributes['count']
			);
		},
		'supports'        => array(
			'autoRegister' => true,
		),
	)
);

Props mcsf, oandregal, ramonopoly, westonruter, wildworks.
Fixes #64639.

Location:
trunk
Files:
2 added
3 edited

Legend:

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

    r61650 r61661  
    31323132    return $arg;
    31333133}
     3134
     3135/**
     3136 * Exposes blocks with autoRegister flag for ServerSideRender in the editor.
     3137 *
     3138 * Detects blocks that have the autoRegister flag set in their supports
     3139 * and passes them to JavaScript for auto-registration with ServerSideRender.
     3140 *
     3141 * @access private
     3142 * @since 7.0.0
     3143 */
     3144function _wp_enqueue_auto_register_blocks() {
     3145    $auto_register_blocks = array();
     3146    $registered_blocks    = WP_Block_Type_Registry::get_instance()->get_all_registered();
     3147
     3148    foreach ( $registered_blocks as $block_name => $block_type ) {
     3149        if ( ! empty( $block_type->supports['autoRegister'] ) && ! empty( $block_type->render_callback ) ) {
     3150            $auto_register_blocks[] = $block_name;
     3151        }
     3152    }
     3153
     3154    if ( ! empty( $auto_register_blocks ) ) {
     3155        wp_add_inline_script(
     3156            'wp-block-library',
     3157            sprintf( 'window.__unstableAutoRegisterBlocks = %s;', wp_json_encode( $auto_register_blocks ) ),
     3158            'before'
     3159        );
     3160    }
     3161}
  • trunk/src/wp-includes/default-filters.php

    r61491 r61661  
    624624add_action( 'enqueue_block_editor_assets', 'wp_enqueue_block_editor_script_modules' );
    625625add_action( 'enqueue_block_editor_assets', 'wp_enqueue_global_styles_css_custom_properties' );
     626add_action( 'enqueue_block_editor_assets', '_wp_enqueue_auto_register_blocks' );
    626627add_action( 'wp_print_scripts', 'wp_just_in_time_script_localization' );
    627628add_filter( 'print_scripts_array', 'wp_prototype_before_jquery' );
  • trunk/src/wp-settings.php

    r61504 r61661  
    395395require ABSPATH . WPINC . '/block-supports/utils.php';
    396396require ABSPATH . WPINC . '/block-supports/align.php';
     397require ABSPATH . WPINC . '/block-supports/auto-register.php';
    397398require ABSPATH . WPINC . '/block-supports/custom-classname.php';
    398399require ABSPATH . WPINC . '/block-supports/generated-classname.php';
Note: See TracChangeset for help on using the changeset viewer.