apply_filters( ‘register_block_type_args’, array $args, string $block_type ) is a WordPress PHP filter that allows you to modify the arguments used for registering a block type in the WordPress editor.
Usage
add_filter( 'register_block_type_args', 'my_function', 10, 2 ); function my_function( $args, $block_type ) { // Your code here to modify the $args array return $args; }
Parameters
- $args (array)
- Array of arguments for registering a block type.
- $block_type (string)
- Block type name including namespace.
Examples
Change block category
add_filter( 'register_block_type_args', 'change_block_category', 10, 2 ); function change_block_category( $args, $block_type ) { if ( 'my-namespace/my-block' === $block_type ) { $args['category'] = 'custom-category'; } return $args; }
This code changes the category of the custom block named my-namespace/my-block
to custom-category
.
Set block as reusable
add_filter( 'register_block_type_args', 'set_block_reusable', 10, 2 ); function set_block_reusable( $args, $block_type ) { if ( 'my-namespace/my-block' === $block_type ) { $args['supports']['reusable'] = true; } return $args; }
This code sets the custom block named my-namespace/my-block
as reusable.
Remove block alignment options
add_filter( 'register_block_type_args', 'remove_block_alignments', 10, 2 ); function remove_block_alignments( $args, $block_type ) { if ( 'my-namespace/my-block' === $block_type ) { $args['supports']['align'] = false; } return $args; }
This code removes alignment options from the custom block named my-namespace/my-block
.
Add custom attributes
add_filter( 'register_block_type_args', 'add_custom_attributes', 10, 2 ); function add_custom_attributes( $args, $block_type ) { if ( 'my-namespace/my-block' === $block_type ) { $args['attributes']['custom_attribute'] = array( 'type' => 'string', 'default' => 'Hello, World!', ); } return $args; }
This code adds a custom attribute named custom_attribute
with a default value of ‘Hello, World!’ to the block my-namespace/my-block
.
Set block as non-dynamic
add_filter( 'register_block_type_args', 'set_block_non_dynamic', 10, 2 ); function set_block_non_dynamic( $args, $block_type ) { if ( 'my-namespace/my-block' === $block_type ) { $args['render_callback'] = null; } return $args; }
This code sets the custom block named my-namespace/my-block
as a non-dynamic block by setting its render_callback
to null.