GCBGutenberg
Control Blocks
v1.3.3
Buy

FAQ

Questions and answers your client adds, in native details elements, with FAQPage structured data. Every file shown in full.

A list of questions and answers your client adds. Each one is a native <details> element, so it opens and closes with no script and works from the keyboard. On the front of the site the block also prints the list as FAQPage structured data. 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/faq/ in your theme. The folder name is the block's name, so this registers as gcb/faq.

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.

blocks/faq/block.json
{	"apiVersion": 3,	"name": "gcb/faq",	"title": "FAQ",	"description": "Questions and answers your client adds. Each one opens and closes, and search engines get the list as structured data.",	"category": "design",	"icon": "editor-help",	"supports": { "html": false },	"style": "file:./style.css"}

block.fields.json lists two fields: a heading, and the questions. The questions are a repeater: a list of rows, each a question and an answer, from 1 to 30 rows. The three rows in default are what a new block starts with.

blocks/faq/block.fields.json
{	"controls": [		{ "id": "c_heading", "type": "text", "label": "Heading", "attributeKey": "heading", "default": "Frequently asked questions",			"validation": { "maxLength": 60 } },		{ "id": "c_items", "type": "repeater", "label": "Questions", "attributeKey": "items",			"addButtonLabel": "Add question", "collapsedTitle": "question", "min": 1, "max": 30,			"fields": [				{ "id": "question", "type": "text", "label": "Question", "attributeKey": "question", "validation": { "required": true } },				{ "id": "answer", "type": "textarea", "label": "Answer", "attributeKey": "answer" }			],			"default": [				{ "_id": "q1", "question": "How long does delivery take?", "answer": "Most orders arrive in three to five working days." },				{ "_id": "q2", "question": "Can I return an order?", "answer": "Yes. Send it back within 30 days for a full refund." },				{ "_id": "q3", "question": "How do I get in touch?", "answer": "Email us or call during business hours. We reply within one working day." }			] }	]}

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.

Here the heading has a marker. The questions don't, so they are kept in the sidebar. A row with no question is left out, and a question with no answer yet is shown but kept out of the structured data.

The <script type="application/ld+json"> at the end is the structured data: the same questions and answers as a schema.org FAQPage. It is printed for visitors only. The editor draws its preview through the REST API, and the template skips the script there. Search engines expect one FAQPage on a page, so use one FAQ block a page.

blocks/faq/render.php
<?php/** * FAQ: a heading and a list of questions, each a native <details> that opens and closes with no script. * * The heading is typed on the page. The questions are rows of a repeater field, kept in the sidebar. * On the front of the site the same rows are printed once more as FAQPage structured data (JSON-LD). * * @var array $attributes The block's field values. */ $heading = (string) ( $attributes['heading'] ?? '' );$items   = array(); foreach ( (array) ( $attributes['items'] ?? array() ) as $row ) {	$row      = (array) $row;	$question = trim( (string) ( $row['question'] ?? '' ) );	$answer   = trim( (string) ( $row['answer'] ?? '' ) );	if ( '' !== $question ) {		$items[] = array( 'question' => $question, 'answer' => $answer );	}} // The editor draws its preview through the REST API. Structured data is for the page only.$in_editor = ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || is_admin();?><section <?php echo get_block_wrapper_attributes( array( 'class' => 'faq-block' ) ); ?>>	<?php if ( '' !== $heading ) : ?>		<h2 class="faq-block__heading" data-gcb-field="heading" data-gcb-field-type="text"><?php echo esc_html( $heading ); ?></h2>	<?php endif; ?> 	<div class="faq-block__list">		<?php foreach ( $items as $item ) : ?>			<details class="faq-block__item">				<summary class="faq-block__question"><?php echo esc_html( $item['question'] ); ?></summary>				<?php if ( '' !== $item['answer'] ) : ?>					<div class="faq-block__answer">						<p><?php echo nl2br( esc_html( $item['answer'] ) ); ?></p>					</div>				<?php endif; ?>			</details>		<?php endforeach; ?>	</div> 	<?php	if ( ! $in_editor ) :		$schema = array(			'@context'   => 'https://schema.org',			'@type'      => 'FAQPage',			'mainEntity' => array(),		);		foreach ( $items as $item ) {			if ( '' === $item['answer'] ) {				continue;			}			$schema['mainEntity'][] = array(				'@type'          => 'Question',				'name'           => $item['question'],				'acceptedAnswer' => array(					'@type' => 'Answer',					'text'  => $item['answer'],				),			);		}		// JSON_HEX_TAG keeps a "</script>" typed into an answer from ending the tag.		if ( $schema['mainEntity'] ) :			?>			<script type="application/ld+json"><?php echo wp_json_encode( $schema, JSON_HEX_TAG | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE ); ?></script>			<?php		endif;	endif;	?></section>

style.css is a plain starting look, loaded by "style": "file:./style.css" in block.json. It swaps the browser's triangle for a plus that turns into a minus. Replace it with your design.

blocks/faq/style.css
.faq-block { max-width: 760px; margin-inline: auto; }.faq-block__heading { margin: 0 0 20px; }.faq-block__list { border-top: 1px solid color-mix(in srgb, currentColor 18%, transparent); }.faq-block__item { border-bottom: 1px solid color-mix(in srgb, currentColor 18%, transparent); }.faq-block__question { position: relative; display: block; padding: 18px 44px 18px 0; font-size: 1.1em; font-weight: 600; line-height: 1.4; cursor: pointer; list-style: none; }.faq-block__question::-webkit-details-marker { display: none; }.faq-block__question::before, .faq-block__question::after { content: ""; position: absolute; right: 6px; top: 50%; width: 14px; height: 2px; margin-top: -1px; background: currentColor; transition: transform .2s; }.faq-block__question::after { transform: rotate(90deg); }.faq-block__item[open] > .faq-block__question::after { transform: rotate(0deg); }.faq-block__question:focus-visible { outline: 2px solid currentColor; outline-offset: 2px; border-radius: 4px; }.faq-block__answer { padding: 0 44px 20px 0; }.faq-block__answer p { margin: 0; line-height: 1.6; opacity: .85; }@media (prefers-reduced-motion: reduce) { .faq-block__question::before, .faq-block__question::after { transition: none; } }

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 and types.
  • Opens Questions in the sidebar and types each question and its answer.
  • Presses Add question for a new one, drags the rows to reorder them, and removes one that no longer applies.

They don't touch the structured data: it follows the questions. They can't save the page with no questions at all, or with a row that has no question.

Use it

  1. Unzip the download.
  2. Copy faq/ into your theme's blocks/ folder.
  3. Reload the block editor. FAQ 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.

Build anything in the block editor

Every licence includes all features and a year of updates and support.

Buy GCB from $8.25 a month See pricing