Fields

Of course, the real usefulness of blocks come from connecting custom fields to them. This way, blocks can be infinitely reused with different settings or texts. While defining fields via the ACF or MB interface is completely fine, Blockstudio includes a handy way to register fields in the block files.

Blockstudio is automatically going to look for a $fields variable inside your files and if available, will register those and connect them to the block.

ACF

See documentation

<?php // cta.php /* Title: Call to Action Description: Call to action element. Category: design Icon: lightbulb Mode: preview SupportsAlign: false SupportsMode: false */ $fields = [ [ 'key' => 'cta_title_key', 'label' => 'Title', 'name' => 'cta_title', 'type' => 'text', ], [ 'key' => 'cta_text_key', 'label' => 'Text', 'name' => 'cta_text', 'type' => 'textarea', ], ); ?> <h1><?php the_field( 'cta_title' ) ?: 'Title'; ?></h1> <p><?php the_field( 'cta_text' ) ?: 'Text'; ?></p>

Metabox

See documentation

<?php // cta.php /* Title: Call to Action Description: Call to action element. Category: design Icon: lightbulb Mode: preview SupportsAlign: false SupportsMode: false */ $fields = [ [ "type" => "text", "id" => "cta_title", "name" => "Title", ], [ "type" => "text", "id" => "cta_text", "name" => "Subtitle", ], ]; ?> <h1><?php mb_the_block_field( 'cta_title' ) ?: 'Title'; ?></h1> <p><?php mb_the_block_field( 'cta_text' ) ?: 'Text'; ?></p>