A
//A
content
// This catches the very common pattern of FAQ sections built
// with standard headings and paragraphs.
// ---------------------------------------------------------------
if ( preg_match_all(
'/ ]*>.*?<\/p>(?:\s* ]*>.*?<\/p>)*)/si',
$content, $matches, PREG_SET_ORDER
) ) {
foreach ( $matches as $m ) {
$pairs[] = $this->build_faq_pair( $m[1], $m[2] );
}
if ( ! empty( $pairs ) ) {
return $pairs;
}
}
// ---------------------------------------------------------------
// 8. Text-based Q:/A: or Question:/Answer: patterns (fallback)
// ---------------------------------------------------------------
$stripped = wp_strip_all_tags( $content );
if ( preg_match_all(
'/(?:Q:|Question:)\s*(.+?)\n+(?:A:|Answer:)\s*(.+?)(?=\n\s*(?:Q:|Question:)|\n\n\n|$)/si',
$stripped, $matches, PREG_SET_ORDER
) ) {
foreach ( $matches as $m ) {
$pairs[] = $this->build_faq_pair( $m[1], $m[2] );
}
}
return $pairs;
}
/**
* Build a single FAQ schema pair (Question + AcceptedAnswer).
*
* @param string $question Raw question text (may contain HTML).
* @param string $answer Raw answer text (may contain HTML).
* @return array Schema.org Question entity.
*/
private function build_faq_pair( $question, $answer ) {
return array(
'@type' => 'Question',
'name' => wp_strip_all_tags( trim( $question ) ),
'acceptedAnswer' => array(
'@type' => 'Answer',
'text' => wp_strip_all_tags( trim( $answer ) ),
),
);
}
// -----------------------------------------------------------------------
// Detection Helpers
// -----------------------------------------------------------------------
/**
* Check if auto-detected schema would fire for this post.
*
* @param \WP_Post $post Post.
* @return bool
*/
private function has_auto_schema( $post ) {
$schema_engine = new Schema();
$result = $schema_engine->get_for_post( $post->ID );
if ( empty( $result['schemas'] ) ) {
return false;
}
// Breadcrumb alone doesn't count as meaningful schema coverage.
foreach ( $result['schemas'] as $schema ) {
$type = isset( $schema['@type'] ) ? $schema['@type'] : '';
if ( $type && 'BreadcrumbList' !== $type ) {
return true;
}
}
return false;
}
/**
* Detect FAQ-style content across all common WordPress patterns.
*
* Checks for: Yoast/Rank Math FAQ blocks, shortcodes, HTML5 details,
* definition lists, accordion markup, question headings, and text Q/A.
*
* @param string $content Post content (raw post_content).
* @return bool
*/
private function has_faq_content( $content ) {
// Gutenberg FAQ blocks (Yoast, Rank Math).
if ( false !== strpos( $content, 'wp:yoast/faq-block' ) ) {
return true;
}
if ( false !== strpos( $content, 'wp:rank-math/faq-block' ) ) {
return true;
}
// FAQ shortcodes ([faq], [q], or third-party like [accordion]).
if ( preg_match( '/\[(faq|accordion|toggle|question|answer)\b/i', $content ) ) {
return true;
}
// Schema FAQ classes from Yoast / Rank Math.
if ( false !== strpos( $content, 'schema-faq-question' ) ) {
return true;
}
if ( false !== strpos( $content, 'rank-math-question' ) ) {
return true;
}
// HTML5 details/summary (native accordion).
if ( false !== strpos( $content, ' /si', $content );
}
}