@wordpress/block-library Edit

Block library for the WordPress editor.

Installation Installation

Install the module

npm install @wordpress/block-library --save

This package assumes that your code will run in an ES2015+ environment. If you’re using an environment that has limited or no support for ES2015+ such as IE browsers then using core-js will add polyfills for these methods.

Top ↑

Building JavaScript for the browser Building JavaScript for the browser

If a frontend.js file is present in the block’s directory, this file will be built along other assets, making it available to load from the browser.

This enables us to, for instance, load this file when the block is present on the page in two ways:

  1. Using the block’s render_callback:
function render_my_block() {
    $script_path = __DIR__ . '/block-name/frontend.js';

    if ( file_exists( $script_path ) ) {
        wp_enqueue_script(
            'my_block_frontend_script',
            plugins_url( 'frontend.js', $script_path ),
            array(),
            false,
            true
        );
    }
}

function register_block_my_block() {
    register_block_type(
        __DIR__ . '/block-name',
        array(
            'render_callback' => 'render_my_block',
        )
    );
}


add_action( 'init', 'register_block_my_block' );
  1. Using the render_block filter:
function render_my_block() {
    $script_path = __DIR__ . '/block-name/frontend.js';

    if ( file_exists( $script_path ) ) {
        wp_enqueue_script(
            'my_block_frontend_script',
            plugins_url( 'frontend.js', $script_path ),
            array(),
            false,
            true
        );
    }
}

apply_filter( 'render_block', 'render_my_block' );

Top ↑

API API

# registerCoreBlocks

Function to register core blocks provided by the block editor.

Usage

import { registerCoreBlocks } from '@wordpress/block-library';

registerCoreBlocks();

Parameters

  • blocks Array: An optional array of the core blocks being registered.

Code is Poetry.