Skip to content
PHP Filters

lzb/prepare_block_attribute

Sets the storage type and default of the Gutenberg attribute behind one control, which a custom control needs when its value is not a plain string.

It runs once per control while the attribute list for register_block_type() is built, after lzb/block_data has assembled the block and before lzb/register_block_type_data sees the finished registration array. Two loops fire it. prepare_block_attributes() handles the controls stored in the block markup, and prepare_block_meta_attributes() handles the ones with Save in Meta on, whose $attribute_data carries the extra source and meta keys.

Attributes

NameTypeDescription
$attribute_dataArraytype and default, plus source and meta for a meta control
$controlArraythis control, merged with its type's defaults
$controlsArrayevery control on the block, keyed by control ID
$kStringcontrol ID of $control inside $controls
$blockArrayblock data, the same array lzb/block_data returns

$attribute_data['type'] comes from the control type's own type property and is one of string, number, boolean, array or object. $attribute_data['default'] is the control's default value, cast to a float when the type is number and the default is not an empty string.

$control always has type, name, default, label, help, child_of, placement, group, width, hide_if_not_selected, required, translate, save_in_meta and save_in_meta_name, because the type's defaults are merged in first. Controls are visited only when their child_of matches the level being built, so a Repeater's inner controls arrive on their own pass.

Usage

PHP
function my_lzb_prepare_block_attribute( $attribute_data, $control, $controls, $k, $block ) {
  // A custom control stores JSON, so register it as an object rather than
  // the string its control class declares.
  if ( 'my-json-control' !== $control['type'] ) {
    return $attribute_data;
  }
 
  $attribute_data['type']    = 'object';
  $attribute_data['default'] = array( 'mode' => 'compact' );
 
  return $attribute_data;
}
 
add_filter( 'lzb/prepare_block_attribute', 'my_lzb_prepare_block_attribute', 10, 5 );

The type declared here is what Gutenberg validates the saved value against, so a mismatch makes the editor report the block as invalid after a reload rather than failing at save time. For a Save in Meta control the same array is also handed to register_meta(). Setting type to array without also setting items makes Lazy Blocks fall back to show_in_rest => true, and WordPress refuses to expose array meta over REST without an items schema. Dropping the default key gives the attribute a null default, which is different from an empty string and shows up in Handlebars as nothing at all.

To change the value at render time instead of the type at registration time, use lzb/control_value.

Was this article helpful?

Copyright © 2026 Lazy Blocks.