custom-block.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * Plugin Name: Custom Block
  4. * Description: Example block scaffolded with Create Block tool.
  5. * Version: 0.1.0
  6. * Requires at least: 6.7
  7. * Requires PHP: 7.4
  8. * Author: The WordPress Contributors
  9. * License: GPL-2.0-or-later
  10. * License URI: https://www.gnu.org/licenses/gpl-2.0.html
  11. * Text Domain: custom-block
  12. *
  13. * @package CreateBlock
  14. */
  15. if ( ! defined( 'ABSPATH' ) ) {
  16. exit; // Exit if accessed directly.
  17. }
  18. /**
  19. * Registers the block using a `blocks-manifest.php` file, which improves the performance of block type registration.
  20. * Behind the scenes, it also registers all assets so they can be enqueued
  21. * through the block editor in the corresponding context.
  22. *
  23. * @see https://make.wordpress.org/core/2025/03/13/more-efficient-block-type-registration-in-6-8/
  24. * @see https://make.wordpress.org/core/2024/10/17/new-block-type-registration-apis-to-improve-performance-in-wordpress-6-7/
  25. */
  26. function create_block_custom_block_block_init() {
  27. /**
  28. * Registers the block(s) metadata from the `blocks-manifest.php` and registers the block type(s)
  29. * based on the registered block metadata.
  30. * Added in WordPress 6.8 to simplify the block metadata registration process added in WordPress 6.7.
  31. *
  32. * @see https://make.wordpress.org/core/2025/03/13/more-efficient-block-type-registration-in-6-8/
  33. */
  34. if ( function_exists( 'wp_register_block_types_from_metadata_collection' ) ) {
  35. wp_register_block_types_from_metadata_collection( __DIR__ . '/build', __DIR__ . '/build/blocks-manifest.php' );
  36. return;
  37. }
  38. /**
  39. * Registers the block(s) metadata from the `blocks-manifest.php` file.
  40. * Added to WordPress 6.7 to improve the performance of block type registration.
  41. *
  42. * @see https://make.wordpress.org/core/2024/10/17/new-block-type-registration-apis-to-improve-performance-in-wordpress-6-7/
  43. */
  44. if ( function_exists( 'wp_register_block_metadata_collection' ) ) {
  45. wp_register_block_metadata_collection( __DIR__ . '/build', __DIR__ . '/build/blocks-manifest.php' );
  46. }
  47. /**
  48. * Registers the block type(s) in the `blocks-manifest.php` file.
  49. *
  50. * @see https://developer.wordpress.org/reference/functions/register_block_type/
  51. */
  52. $manifest_data = require __DIR__ . '/build/blocks-manifest.php';
  53. foreach ( array_keys( $manifest_data ) as $block_type ) {
  54. register_block_type( __DIR__ . "/build/{$block_type}" );
  55. }
  56. }
  57. add_action( 'init', 'create_block_custom_block_block_init' );