diff --git a/docs/designers-developers/developers/tutorials/create-block/wp-plugin.md b/docs/designers-developers/developers/tutorials/create-block/wp-plugin.md index abfe91bc62ea2e..b044423658967a 100644 --- a/docs/designers-developers/developers/tutorials/create-block/wp-plugin.md +++ b/docs/designers-developers/developers/tutorials/create-block/wp-plugin.md @@ -92,57 +92,27 @@ To load the built script, so it is run within the editor, you need to tell WordP ```php function create_block_gutenpride_block_init() { - $dir = __DIR__; - - $script_asset_path = "$dir/build/index.asset.php"; - if ( ! file_exists( $script_asset_path ) ) { - throw new Error( - 'You need to run `npm start` or `npm run build` for the "create-block/gutenpride" block first.' - ); - } - $index_js = 'build/index.js'; - $script_asset = require( $script_asset_path ); - wp_register_script( - 'create-block-gutenpride-block-editor', - plugins_url( $index_js, __FILE__ ), - $script_asset['dependencies'], - $script_asset['version'] - ); - wp_set_script_translations( 'create-block-gutenpride-block-editor', 'gutenpride' ); - - $editor_css = 'editor.css'; - wp_register_style( - 'create-block-gutenpride-block-editor', - plugins_url( $editor_css, __FILE__ ), - array(), - filemtime( "$dir/$editor_css" ) - ); - - $style_css = 'style.css'; - wp_register_style( - 'create-block-gutenpride-block', - plugins_url( $style_css, __FILE__ ), - array(), - filemtime( "$dir/$style_css" ) - ); - - register_block_type( 'create-block/gutenpride', array( - 'apiVersion' => 2, - 'editor_script' => 'create-block-gutenpride-block-editor', - 'editor_style' => 'create-block-gutenpride-block-editor', - 'style' => 'create-block-gutenpride-block', - ) ); + register_block_type_from_metadata( __DIR__ ); } add_action( 'init', 'create_block_gutenpride_block_init' ); ``` -The build process creates a secondary asset file that contains the list of dependencies and a file version based on the timestamp, this is the `index.asset.php` file. +The `register_block_type_from_metadata` function registers the block we are going to create and specifies the `editor_script` file handle registered from the metadata provided in `block.json` file. So now when the editor loads it will load this script. -The `wp_register_script` function registers a name, called the handle, and relates that name to the script file. The dependencies are used to specify if the script requires including other libraries. The version is specified so the browser will reload if the file changed. +```json +{ + "apiVersion": 2, + "name": "create-block/gutenpride", + "title": "Gutenpride", + "editorScript": "file:./build/index.js" +} +``` -The `wp_set_script_translations` function tells WordPress to load translations for this script, if they exist. See more about [translations & internationalization.](/docs/designers-developers/developers/internationalization.md) +For the `editorScript` provided in the block metadata, the build process creates a secondary asset file that contains the list of dependencies and a file version based on the timestamp, this is the `index.asset.php` file. + +The `wp_register_script` function used internally registers a name, called the handle, and relates that name to the script file. The dependencies are used to specify if the script requires including other libraries. The version is specified so the browser will reload if the file changed. -The `register_block_type` function registers the block we are going to create and specifies the editor_script file handle registered. So now when the editor loads it will load this script. +The `wp_set_script_translations` function tells WordPress to load translations for this script, if they exist. See more about [translations & internationalization.](/docs/designers-developers/developers/internationalization.md) With the above in place, create a new post to load the editor and check your plugin is in the inserter. You can use `/` to search, or click the box with the [+] and search for "Gutenpride" to find the block.