Blocks

Helpers
in package

Global helper functions

Table of Contents

Methods

add_dynamic_context_to_blocks()  : void
Adds dynamic context to blocks.
get_block_attribute()  : mixed|null
Get a block attribute value from the current block context or the current AJAX render request.
get_block_by_child_block_name()  : Block|null
Gets the block by child block name.
get_block_by_name()  : Block|null
Gets the single block instance by block name.
get_child_block_by_name()  : Child_Block|null
Gets a child block by name.
get_child_block_by_path()  : Child_Block
Finds a child block from an array of existing child blocks based on its path.
get_current_block_name()  : string
Gets the current block name.
render_blocks()  : mixed
Renders and sanitizes a content string of blocks.
render_blocks_in_post_context()  : mixed
Renders a content string (of blocks) in the context of a post.
render_blocks_with_dynamic_context()  : mixed
Renders blocks with dynamic context.
render_inner_blocks_in_post_context()  : mixed
Renders inner blocks within the context of a post.
replace_child_block_by_path()  : array<string|int, Child_Block>
Targets a child block by path from existing child blocks and replaces it with a new one provided.
set_acf_block_mode()  : mixed
Sets the ACF block mode.
set_current_block_name()  : mixed
Sets the current block name.
set_default_block_category()  : mixed
Sets the default category for new blocks. Must be called before blocks are initialized.

Methods

add_dynamic_context_to_blocks()

Adds dynamic context to blocks.

public static add_dynamic_context_to_blocks(array<string|int, mixed> &$blocks[, array<string|int, mixed> $dynamic_context = array() ]) : void

This will add an attribute called dynamic_context to all nested blocks.

Parameters
$blocks : array<string|int, mixed>

An array of block structures to add dynamic context to.

$dynamic_context : array<string|int, mixed> = array()

The dynamic context to add to the blocks.

get_block_attribute()

Get a block attribute value from the current block context or the current AJAX render request.

public static get_block_attribute(string $attribute_name[, WP_Block|null $wp_block = null ]) : mixed|null
Parameters
$attribute_name : string

The attribute name.

$wp_block : WP_Block|null = null

The current WordPress block object. Defaults to null if not provided.

Return values
mixed|null

The attribute value, null if the attribute is not found.

get_block_by_child_block_name()

Gets the block by child block name.

public static get_block_by_child_block_name(string $name) : Block|null
Parameters
$name : string

The child block name.

Return values
Block|null

The block instance or null if it cannot be found.

get_block_by_name()

Gets the single block instance by block name.

public static get_block_by_name(string $name) : Block|null
Parameters
$name : string

The block name.

Return values
Block|null

Block instance or null if it cannot be found.

get_child_block_by_name()

Gets a child block by name.

public static get_child_block_by_name(string $name) : Child_Block|null
Parameters
$name : string

The child block name.

Return values
Child_Block|null

The child block instance or null if it cannot be found.

get_child_block_by_path()

Finds a child block from an array of existing child blocks based on its path.

public static get_child_block_by_path(array<string|int, Child_Block$existing_child_blocks, string $path) : Child_Block
Parameters
$existing_child_blocks : array<string|int, Child_Block>

An array of existing child blocks to search in.

$path : string

A "/" separated path to block for example "table/row/cell".

Tags
throws
InvalidArgumentException

If we cannot parse the data provided or find the block.

Return values
Child_Block

get_current_block_name()

Gets the current block name.

public static get_current_block_name() : string
Return values
string

The name of the current block.

render_blocks()

Renders and sanitizes a content string of blocks.

public static render_blocks(string|array<string|int, mixed> $content) : mixed
Parameters
$content : string|array<string|int, mixed>

$content The content string, or array of block structures.

render_blocks_in_post_context()

Renders a content string (of blocks) in the context of a post.

public static render_blocks_in_post_context(string|array<string|int, mixed> $content, int $post_id) : mixed
Parameters
$content : string|array<string|int, mixed>

The content string, or array of block structures.

$post_id : int

The ID of the context post.

render_blocks_with_dynamic_context()

Renders blocks with dynamic context.

public static render_blocks_with_dynamic_context(array<string|int, mixed> $blocks[, array<string|int, mixed> $dynamic_context = array() ]) : mixed

This will add an attribute called dynamic_context to all nested blocks.

Parameters
$blocks : array<string|int, mixed>

An array of block structures to render.

$dynamic_context : array<string|int, mixed> = array()

The dynamic context to add to the blocks.

render_inner_blocks_in_post_context()

Renders inner blocks within the context of a post.

public static render_inner_blocks_in_post_context(WP_Block $wp_block, int $post_id) : mixed
Parameters
$wp_block : WP_Block

The block whose inner blocks should be rendered. Available within render templates.

$post_id : int

The ID of the context post.

replace_child_block_by_path()

Targets a child block by path from existing child blocks and replaces it with a new one provided.

public static replace_child_block_by_path(array<string|int, Child_Block$existing_child_blocks, string $path, Child_Block $new_child_block) : array<string|int, Child_Block>
Parameters
$existing_child_blocks : array<string|int, Child_Block>

An array of existing child blocks to search in.

$path : string

A "/" separated path to block for example "table/row/cell".

$new_child_block : Child_Block

Child block to replace.

Return values
array<string|int, Child_Block>

Array of amended child blocks.

set_acf_block_mode()

Sets the ACF block mode.

public static set_acf_block_mode(string $block_name[, string $mode = 'preview' ]) : mixed
Parameters
$block_name : string

The full block name including vendor prefix.

$mode : string = 'preview'

The ACF block mode.

set_current_block_name()

Sets the current block name.

public static set_current_block_name(string $block_name) : mixed
Parameters
$block_name : string

The name of the current block.

set_default_block_category()

Sets the default category for new blocks. Must be called before blocks are initialized.

public static set_default_block_category(string $category) : mixed
Parameters
$category : string

The default category for new blocks.


        
On this page

Search results