Skip to content
  • Quick Ref
  • Contact
  • About
wpcanyon.com

wpcanyon.com

Auto‑add FAQ schema to posts via PHP (no plugin)

Posted on August 19, 2025 By Admin No Comments on Auto‑add FAQ schema to posts via PHP (no plugin)

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

  1. Create a JSON‑LD template for your FAQ schema.
  2. Hook into the save_post action to append the FAQ schema to your post content.
  3. 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

  1. Prepare your FAQ data: Store your FAQs as post meta under _faq_items. The format should be an array of arrays, each with question and answer keys.
  2. [
      [
        '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.'
      ]
    ]
    
  3. Add the JSON‑LD template function: Place the get_faq_schema_json_ld() function in your theme’s functions.php or a custom plugin.
  4. Hook into save_post: Add the auto_add_faq_schema_on_save() function to save the JSON‑LD schema in post meta when the post is saved.
  5. Output the schema in the front-end: Use the print_faq_schema_json_ld() function hooked to wp_head to print the JSON‑LD script tag.
  6. Test and validate: Use Google’s Rich Results Test to ensure your FAQ schema is correctly recognized.

Works on

Automation & Plugins Tags:PHP, Schema FAQ, SERP

Post navigation

Previous Post: BunnyCDN + Cloudflare: should you double‑up and how to do it
Next Post: Serve WebP in WordPress without breaking Safari

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Top WordPress Themes for Blogs in 2025
  • WordPress Admin Panel Trick: Adding ID Field to the Posts Listing
  • Solution previous_posts_link and next_posts_link Not Working
  • Show Top Commentators in WordPress Without a Plugin
  • How to Style Admin Comments in WordPress

Recent Comments

    Archives

    • August 2025

    Categories

    • Admin & Blocks
    • Admin & UI
    • Automation
    • Automation & Plugins
    • Comments
    • Comparisons
    • Database & Revisions
    • Developer Snippets
    • Fixes & Errors
    • Media & Thumbnails
    • Queries & Pagination
    • Security
    • Speed & Security
    • Tips & Tricks
    • WooCommerce How‑tos
    • WordPress Snippets
    • WordPress Themes
    • Terms & Conditions
    • Affiliate Disclosure

    Copyright © 2025 wpcanyon.com.

    Powered by PressBook WordPress theme

    Also by the maker of MySurveyReviews.com
    Environment Compatibility