alleyinteractive/wp-block-converter

Convert HTML into Gutenberg Blocks with PHP

v1.0.0 2022-12-20 17:54 UTC

This package is auto-updated.

Last update: 2023-03-20 18:33:26 UTC


README

Coding Standards Testing Suite

Convert HTML into Gutenberg Blocks with PHP

Installation

You can install the package via composer:

composer require alleyinteractive/wp-block-converter

Usage

Use this package like so to convert HTML into Gutenberg Blocks:

use Alley\WP\Block_Converter\Block_Converter;

$converter = new Block_Converter();

$blocks = $converter->convert( '<p>Some HTML</p>' );

Filtering the Blocks

The blocks can be filtered on a block-by-block basis or for an entire HTML body.

wp_block_converter_block

Filter the generated block for a specific node.

use Alley\WP\Block_Converter\Block;

add_filter( 'wp_block_converter_block', function ( Block $block, \DOMElement $node ): ?Block {
	// Modify the block before it is serialized.
	$block->content = '...';
	$block->blockName = '...';
	$block->attributes = [ ... ];

	return $block;
}, 10, 2 );

wp_block_converter_html_content

Filter the generated blocks for an entire HTML body.

add_filter( 'wp_block_converter_document_html', function( string $blocks, \DOMNodeList $content ): string {
	// ...
	return $blocks;
}, 10, 2 );

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

This project is actively maintained by Alley Interactive. Like what you see? Come work with us.

License

The GNU General Public License (GPL) license. Please see License File for more information.