Define FAQ question and answer pairs in a custom field and output FAQPage JSON-LD schema automatically, enabling Google's FAQ rich results without a plugin.
// FAQ items are stored as JSON in the '_nahnu_faq' field:
// [{"question":"What is X?","answer":"X is..."}]
add_action( 'wp_head', function (): void {
if ( ! is_singular() ) {
return;
}
$items = json_decode( (string) get_post_meta( get_queried_object_id(), '_nahnu_faq', true ), true );
if ( ! is_array( $items ) || ! $items ) {
return;
}
$entities = [];
foreach ( $items as $item ) {
if ( empty( $item['question'] ) || empty( $item['answer'] ) ) {
continue;
}
$entities[] = [
'@type' => 'Question',
'name' => wp_strip_all_tags( $item['question'] ),
'acceptedAnswer' => [ '@type' => 'Answer', 'text' => wp_kses_post( $item['answer'] ) ],
];
}
if ( ! $entities ) {
return;
}
echo '<script type="application/ld+json">' . wp_json_encode( [
'@context' => 'https://schema.org',
'@type' => 'FAQPage',
'mainEntity' => $entities,
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG ) . "</script>\n";
} );
// A simple box to edit the JSON on posts and pages.
add_action( 'add_meta_boxes', function (): void {
add_meta_box( 'nahnu_faq', 'FAQ schema', function ( WP_Post $post ): void {
wp_nonce_field( 'nahnu_faq_save', 'nahnu_faq_nonce' );
echo '<p>JSON list of {"question": "...", "answer": "..."} items.</p>';
printf( '<textarea name="nahnu_faq" style="width:100%%;min-height:140px;font-family:monospace">%s</textarea>', esc_textarea( (string) get_post_meta( $post->ID, '_nahnu_faq', true ) ) );
}, [ 'post', 'page' ], 'side' );
} );
add_action( 'save_post', function ( int $post_id ): void {
if ( ! isset( $_POST['nahnu_faq_nonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['nahnu_faq_nonce'] ) ), 'nahnu_faq_save' ) || ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
$json = trim( (string) wp_unslash( $_POST['nahnu_faq'] ?? '' ) );
if ( '' === $json ) {
delete_post_meta( $post_id, '_nahnu_faq' );
} elseif ( is_array( json_decode( $json, true ) ) ) {
update_post_meta( $post_id, '_nahnu_faq', wp_slash( $json ) );
}
} );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
FAQPage structured data describes a list of questions and answers on a page in a way machines can read. This snippet adds a box to posts and pages where you paste the questions and answers as JSON, and prints the matching schema in the page head.
Since August 2023 Google only shows FAQ rich results for a small number of well-known government and health websites. For most sites the FAQ dropdowns won’t appear in Google search any more. The markup is still valid schema.org data, and other search engines and AI assistants can use it to understand your page, but don’t add it expecting extra space in Google results.
The JSON is stored in a hidden custom field. On save, the snippet checks the nonce and the user’s permission, and only stores the value if it is valid JSON, so a typo can’t break the schema. On the front end, each item needs both a question and an answer; incomplete ones are skipped. Questions are plain text and answers allow basic HTML such as links, run through wp_kses_post().
Add two questions to a page, save, and run the URL through the Schema Markup Validator at validator.schema.org. It should find one FAQPage with two questions.