Auto‑add FAQ schema to posts via PHP (no plugin)
If you want to add FAQ schema PHP no plugin WordPress to your posts automatically, this tutorial shows you how to do it with clean PHP code. Adding FAQ schema helps search engines understand your content better and can improve your search results with rich snippets. The quick fix is to hook into WordPress’s save_post
action and inject JSON‑LD structured data directly into your post content or metadata without relying on any plugin.
Quick Fix
- Create a JSON‑LD template for your FAQ schema.
- Hook into the
save_post
action to append the FAQ schema to your post content. - Validate your schema using Google’s Rich Results Test.
JSON‑LD template
Here is a simple JSON‑LD template for FAQ schema. This example assumes you have an array of questions and answers stored or generated dynamically.
<?php
function get_faq_schema_json_ld( $faqs ) {
$faq_items = array();
foreach ( $faqs as $faq ) {
$faq_items[] = array(
'@type' => 'Question',
'name' => $faq['question'],
'acceptedAnswer' => array(
'@type' => 'Answer',
'text' => $faq['answer'],
),
);
}
$faq_schema = array(
'@context' => 'https://schema.org',
'@type' => 'FAQPage',
'mainEntity' => $faq_items,
);
return wp_json_encode( $faq_schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
}
?>
Hook into save_post
To automatically add FAQ schema when a post is saved, hook into save_post
. This example assumes FAQs are stored as post meta with a specific key (_faq_items
) as a serialized array of question-answer pairs.
<?php
function auto_add_faq_schema_on_save( $post_id ) {
// Avoid recursion and autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Verify user permissions
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
// Only apply to posts (change if needed)
if ( get_post_type( $post_id ) !== 'post' ) {
return;
}
// Get FAQs from post meta (expected format: array of arrays with 'question' and 'answer')
$faqs = get_post_meta( $post_id, '_faq_items', true );
if ( empty( $faqs ) || ! is_array( $faqs ) ) {
return;
}
// Generate JSON-LD FAQ schema
$faq_json_ld = get_faq_schema_json_ld( $faqs );
// Save JSON-LD as post meta for later use or output
update_post_meta( $post_id, '_faq_schema_json_ld', $faq_json_ld );
}
add_action( 'save_post', 'auto_add_faq_schema_on_save' );
?>
To output the FAQ schema in the front-end, add this to your theme’s header.php
or preferably in wp_head
hook:
<?php
function print_faq_schema_json_ld() {
if ( is_singular( 'post' ) ) {
global $post;
$faq_json_ld = get_post_meta( $post->ID, '_faq_schema_json_ld', true );
if ( $faq_json_ld ) {
echo '<script type="application/ld+json">' . $faq_json_ld . '</script>';
}
}
}
add_action( 'wp_head', 'print_faq_schema_json_ld' );
?>
Validation
After implementing the code, validate your FAQ schema with these steps:
- Publish or update a post with FAQ data saved in the
_faq_items
meta. - Visit the post on the front-end and view the page source to confirm the JSON‑LD script is output.
- Copy the JSON‑LD content and test it using Google’s Rich Results Test.
- Fix any errors or warnings reported by the tool.
Make sure your FAQ questions and answers are clear, concise, and properly formatted in the meta.
Why this happens
WordPress does not automatically add FAQ schema to posts because FAQ content structure varies widely and is often custom. Plugins can do this but add overhead and dependencies. By hooking into save_post
, you can programmatically generate and store FAQ schema JSON‑LD when content is saved, ensuring schema is always up-to-date without manual intervention or plugins.
Storing the JSON‑LD in post meta allows you to separate schema generation from output, improving performance and maintainability.
Step-by-step
- Prepare your FAQ data: Store your FAQs as post meta under
_faq_items
. The format should be an array of arrays, each withquestion
andanswer
keys. - Add the JSON‑LD template function: Place the
get_faq_schema_json_ld()
function in your theme’sfunctions.php
or a custom plugin. - Hook into
save_post
: Add theauto_add_faq_schema_on_save()
function to save the JSON‑LD schema in post meta when the post is saved. - Output the schema in the front-end: Use the
print_faq_schema_json_ld()
function hooked towp_head
to print the JSON‑LD script tag. - Test and validate: Use Google’s Rich Results Test to ensure your FAQ schema is correctly recognized.
[
[
'question' => 'What is WordPress?',
'answer' => 'WordPress is a free and open-source content management system.'
],
[
'question' => 'How to add FAQ schema?',
'answer' => 'By hooking into save_post and generating JSON-LD schema.'
]
]
Works on
Environment | Compatibility |
---|