A call to action: a heading, a line of text and one or two buttons in a band of colour. Your client types the words on the page and picks one of three tones. It is three plain files in your theme and a stylesheet.
Download example A zip of one block folder for your theme. Needs GCB 1.3.1 or later, and nothing else.
The whole block
Create blocks/call-to-action/ in your theme. The folder name is the block's name, so this registers as gcb/call-to-action.
block.json is WordPress's standard block metadata. There are no attributes in it and no render key: GCB makes the attributes from the fields and finds render.php by itself.
{ "apiVersion": 3, "name": "gcb/call-to-action", "title": "Call to action", "description": "A heading, a line of text and one or two buttons in a band of colour. Your client types the words on the page and picks the tone.", "category": "design", "icon": "megaphone", "supports": { "html": false, "align": [ "wide", "full" ] }, "style": "file:./style.css"}block.fields.json lists the fields, in three groups that become panels in the sidebar. The heading is required and limited to 80 characters. Each button is one url field, which stores the address, the label and whether it opens in a new tab. Tone is a toggle-group: a row of three choices that stores the chosen one's value as a string.
{ "controls": [ { "id": "p_words", "type": "group", "label": "Words" }, { "id": "p_buttons", "type": "group", "label": "Buttons" }, { "id": "p_look", "type": "group", "label": "Look" }, { "id": "c_heading", "type": "text", "label": "Heading", "attributeKey": "heading", "default": "Ready to get started?", "parentPanelId": "p_words", "validation": { "required": true, "maxLength": 80 } }, { "id": "c_text", "type": "textarea", "label": "Text", "attributeKey": "text", "default": "Tell us what you need and we will reply within one working day.", "parentPanelId": "p_words", "validation": { "maxLength": 200 } }, { "id": "c_button", "type": "url", "label": "Button", "attributeKey": "button", "parentPanelId": "p_buttons", "default": { "url": "/contact", "text": "Get in touch", "opensInNewTab": false } }, { "id": "c_second", "type": "url", "label": "Second button", "attributeKey": "secondButton", "parentPanelId": "p_buttons", "helpText": "Leave its label empty for one button.", "default": { "url": "", "text": "", "opensInNewTab": false } }, { "id": "c_tone", "type": "toggle-group", "label": "Tone", "attributeKey": "tone", "default": "dark", "parentPanelId": "p_look", "options": [ { "value": "dark", "label": "Dark" }, { "value": "light", "label": "Light" }, { "value": "accent", "label": "Accent" } ] } ]}render.php is the template, for the editor and the page alike. It reads the values from $attributes. An element with data-gcb-field="<attributeKey>" and data-gcb-field-type="<type>" is edited where it sits on the page. A field with no marker appears in the block's sidebar.
Tone has no marker, so it is a setting in the sidebar. The template checks it against the three values it knows and falls back to dark. A button is printed only when it has a label, so the second button stays off the page until your client gives it one. A button that opens in a new tab gets rel="noopener noreferrer".
<?php/** * Call to action: a heading, a line of text and one or two buttons, in one of three tones. * * The heading, the text and each button's label are typed on the page. The second button is printed only when * it has a label. Tone has no marker, so it is a setting in the block's sidebar. * * @var array $attributes The block's field values. */ // A field that stores an object (a link) can arrive as an object or an array.$as_array = static function ( $value ) { return is_object( $value ) ? json_decode( wp_json_encode( $value ), true ) : (array) $value;}; $tones = array( 'dark', 'light', 'accent' );$tone = in_array( $attributes['tone'] ?? '', $tones, true ) ? $attributes['tone'] : 'dark';$text = (string) ( $attributes['text'] ?? '' );$buttons = array( 'button' => $as_array( $attributes['button'] ?? array() ), 'secondButton' => $as_array( $attributes['secondButton'] ?? array() ),);$buttons = array_filter( $buttons, static function ( $button ) { return ! empty( $button['text'] ); });?><section <?php echo get_block_wrapper_attributes( array( 'class' => 'call-to-action call-to-action--' . $tone ) ); ?>> <h2 class="call-to-action__heading" data-gcb-field="heading" data-gcb-field-type="text"><?php echo esc_html( $attributes['heading'] ?? '' ); ?></h2> <?php if ( '' !== $text ) : ?> <p class="call-to-action__text" data-gcb-field="text" data-gcb-field-type="textarea"><?php echo esc_html( $text ); ?></p> <?php endif; ?> <?php if ( $buttons ) : ?> <p class="call-to-action__buttons"> <?php foreach ( $buttons as $key => $button ) : ?> <a class="call-to-action__button<?php echo 'secondButton' === $key ? ' call-to-action__button--second' : ''; ?>" data-gcb-field="<?php echo esc_attr( $key ); ?>" data-gcb-field-type="url" href="<?php echo esc_url( $button['url'] ?? '#' ); ?>"<?php echo ! empty( $button['opensInNewTab'] ) ? ' target="_blank" rel="noopener noreferrer"' : ''; ?>><?php echo esc_html( $button['text'] ); ?></a> <?php endforeach; ?> </p> <?php endif; ?></section>style.css is a plain starting look, loaded by "style": "file:./style.css" in block.json. Replace it with your design.
.call-to-action { padding: clamp(36px, 6vw, 72px) clamp(24px, 5vw, 64px); border-radius: 14px; text-align: center; background: #1d1b18; color: #fff; }.call-to-action--light { background: #f3efe8; color: #1d1b18; }.call-to-action--accent { background: #2f55c9; color: #fff; }.call-to-action__heading { margin: 0 auto 12px; max-width: 22ch; font-size: clamp(28px, 3.4vw, 44px); line-height: 1.1; color: inherit; }.call-to-action__text { margin: 0 auto 28px; max-width: 48ch; font-size: 18px; line-height: 1.6; opacity: .85; }.call-to-action__buttons { margin: 0; display: flex; flex-wrap: wrap; justify-content: center; gap: 12px; }.call-to-action__button { padding: 14px 28px; border: 1px solid #fff; border-radius: 999px; background: #fff; color: #1d1b18; text-decoration: none; }.call-to-action__button--second { background: transparent; color: inherit; border-color: currentColor; }.call-to-action--light .call-to-action__button { border-color: #1d1b18; background: #1d1b18; color: #fff; }.call-to-action--light .call-to-action__button--second { background: transparent; color: inherit; }.call-to-action__button:focus-visible { outline: 2px solid currentColor; outline-offset: 3px; }That is all of it. There is no edit.js, no npm and no build step: save the files, reload the editor, and the block is in the inserter.
What your client does
- Clicks the heading or the text and types.
- Changes the button's label and address.
- Gives the second button a label and an address in the Buttons panel when they want two.
- Picks Dark, Light or Accent under Tone in the sidebar.
They can't save the page with no heading, and they can't pick a colour that isn't one of yours.
Use it
- Unzip the download.
- Copy
call-to-action/into your theme'sblocks/folder. - Reload the block editor. Call to action is in the inserter.
That's all of it. You don't register the block: there's no register_block_type() call and nothing to add to functions.php. GCB registers every folder in your theme's blocks/ folder by itself.
More examples: all examples, or download them together as the GCB Kit. To build one from nothing, see how to build a block.