Skip to content

Instantly share code, notes, and snippets.

@diggeddy
Last active March 9, 2023 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diggeddy/4845bf8d1230b191fd27fd200a6bd235 to your computer and use it in GitHub Desktop.
Save diggeddy/4845bf8d1230b191fd27fd200a6bd235 to your computer and use it in GitHub Desktop.
Create FAQ json-ld schema from GB Pro Accordion block
<?php
function create_faq_schema($content) {
// get the bloocks
global $post;
$blocks = parse_blocks( $post->post_content );
// create arrays to store our questions
$faq = array();
$questions = array();
foreach ($blocks as $block) {
// loop through our blocks
if (isset($block['attrs']['className']) && $block['blockName'] === 'generateblocks/container' && in_array('faqs-wrap', explode(' ', $block['attrs']['className']))) {
// if the the GB Accordion Block parent container has faqs-wrap class
// get the inner blocks of container
$faq_blocks = $block['innerBlocks'];
foreach ($faq_blocks as $faq_block) {
// loop through our inner blocks
if ($faq_block['blockName'] === 'generateblocks/container' && in_array('faq-item', explode(' ', $faq_block['attrs']['className']))) {
// if accordion item has a class of faq-item
// get the inner blocks
$faq_item_blocks = $faq_block['innerBlocks'];
// prepare our $question and $answer vars
$question = '';
$answer = '';
foreach ($faq_item_blocks as $faq_item_block) {
// loop through out inner blocks
if ($faq_item_block['blockName'] === 'generateblocks/button' && in_array('question', explode(' ', $faq_item_block['attrs']['className']))) {
// if the accordion button has class of question
// add raw text of innerHTML to $question
$question = wp_strip_all_tags($faq_item_block['innerHTML']);
} elseif ($faq_item_block['blockName'] === 'generateblocks/container' && in_array('answer-wrap', explode(' ', $faq_item_block['attrs']['className']))) {
// if the accordion content has a class of answer-wrap
// get its inner blocks
$answer_blocks = $faq_item_block['innerBlocks'];
foreach ($answer_blocks as $answer_block) {
if ($answer_block['blockName'] === 'generateblocks/headline' && in_array('answer', explode(' ', $answer_block['attrs']['className']))) {
// if an inner headline block has a class of answer
// add raw text in innerHMTL to $answer
$answer = wp_strip_all_tags($answer_block['innerHTML']);
}
}
}
}
// add our $question and $answer to our $questions
if (!empty($question) && !empty($answer)) {
$q = array(
'@type' => 'Question',
'name' => $question,
'acceptedAnswer' => array(
'@type' => 'Answer',
'text' => $answer
)
);
$questions[] = $q;
}
}
}
}
}
// build out ld+json schema
$faq['@context'] = 'https://schema.org';
$faq['@type'] = 'FAQPage';
$faq['mainEntity'] = $questions;
$json_ld = '<script type="application/ld+json">' . json_encode($faq) . '</script>';
echo $json_ld;
}
add_action('wp_head', 'create_faq_schema', 999 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment