Skip to content

Add BlogPosting Schema in WordPress Without a Plugin (Simple Way)

By ali 9 min read

Illustration showing BlogPosting schema and structured data being added to a WordPress blog without a plugin

Want to add BlogPosting schema to your WordPress website without installing another plugin? You can do it with a small PHP snippet.

BlogPosting schema is a type of structured data that helps search engines understand important details about your articles, including the headline, author, publication date, modified date, and featured image.

Many WordPress SEO plugins generate schema automatically. However, if you manage your website’s SEO and custom functionality yourself, you can also add this markup directly using WordPress functions.

In this tutorial, you’ll learn how to generate BlogPosting schema automatically for all your standard WordPress posts using JSON-LD, without manually editing every article.

What Is BlogPosting Schema?

BlogPosting is a structured data type defined by Schema.org. It describes a blog article in a format that search engines can understand.

It can provide information such as:

  • Article headline
  • Article description
  • Author name
  • Publication date
  • Last modified date
  • Featured image
  • Publisher information
  • Article URL

Google supports Article structured data, including the BlogPosting subtype. This markup can help Google understand your article and may make it eligible for certain enhanced search appearances. However, adding schema does not guarantee rich results, better rankings, or faster indexing.

Why Use JSON-LD for WordPress Schema?

JSON-LD is a structured data format recommended by Google. It allows you to describe your article without changing the visible content or HTML layout of your page.

Here are some reasons to use it:

  • Easy to maintain: You can generate schema using existing WordPress data.
  • Separate from page content: The JSON-LD script does not need to be mixed into your article HTML.
  • Automatic: One PHP snippet can generate schema for multiple posts.
  • Flexible: You can add more properties when your website needs them.

Where Should You Add the PHP Code?

You can add the code to your WordPress child theme’s functions.php file or include it in a custom PHP file that your theme loads.

A child theme is recommended because updates to the parent theme will not overwrite your custom code.

If you prefer to keep custom functionality separate from your theme, you can also create a site-specific custom plugin. See our guide on how to create a custom plugin in WordPress.

Add BlogPosting Schema in WordPress Without a Plugin

Copy the following PHP snippet and add it to your child theme’s functions.php file.

add_action('wp_head', function () {

    // Only generate schema for individual blog posts.
    if (!is_singular('post')) {
        return;
    }

    $post = get_queried_object();

    if (!$post instanceof WP_Post) {
        return;
    }

    // Get the article description.
    $description = get_the_excerpt($post);

    // Generate a description if the post has no excerpt.
    if (empty($description)) {
        $content = strip_shortcodes($post->post_content);

        $description = wp_trim_words(
            wp_strip_all_tags($content),
            35,
            '...'
        );
    }

    // Get the author name.
    $author_name = get_the_author_meta(
        'display_name',
        $post->post_author
    );

    // Get the featured image URL.
    $featured_image = get_the_post_thumbnail_url(
        $post->ID,
        'full'
    );

    // Build the BlogPosting schema.
    $schema = array(
        '@context' => 'https://schema.org',
        '@type'    => 'BlogPosting',

        'headline' => get_the_title($post),

        'description' => wp_strip_all_tags($description),

        'author' => array(
            '@type' => 'Person',
            'name'  => $author_name
        ),

        'datePublished' => get_post_time(
            'c',
            false,
            $post
        ),

        'dateModified' => get_post_modified_time(
            'c',
            false,
            $post
        ),

        'mainEntityOfPage' => array(
            '@type' => 'WebPage',
            '@id'   => get_permalink($post)
        ),

        'publisher' => array(
            '@type' => 'Organization',
            'name'  => get_bloginfo('name')
        )
    );

    // Add the featured image when available.
    if ($featured_image) {
        $schema['image'] = array($featured_image);
    }

    // Convert the schema array into valid JSON.
    $json_schema = wp_json_encode(
        $schema,
        JSON_UNESCAPED_SLASHES |
        JSON_UNESCAPED_UNICODE |
        JSON_HEX_TAG |
        JSON_HEX_AMP |
        JSON_HEX_APOS |
        JSON_HEX_QUOT
    );

    // Output the JSON-LD script in the page head.
    if ($json_schema) {
        echo "\n<script type=\"application/ld+json\">";
        echo $json_schema;
        echo "</script>\n";
    }

});

That’s it! WordPress will now generate BlogPosting JSON-LD schema automatically for individual blog posts.

The snippet retrieves the article information directly from WordPress, so you don’t need to enter the title, author, dates, or featured image manually for every post.

How Does the BlogPosting Schema Code Work?

Let’s understand the important parts of the PHP snippet.

1. Check Whether the Current Page Is a Blog Post

if (!is_singular('post')) {
    return;
}

The is_singular('post') conditional checks whether the current page is an individual standard WordPress post.

If the visitor opens a homepage, category archive, regular page, or another post type, the function stops without generating BlogPosting schema.

2. Retrieve the Current Post

$post = get_queried_object();

if (!$post instanceof WP_Post) {
    return;
}

This retrieves the current queried object and checks that it is a WordPress post. We use this post object to retrieve the article’s information.

3. Generate the Article Description

$description = get_the_excerpt($post);

if (empty($description)) {
    $content = strip_shortcodes($post->post_content);

    $description = wp_trim_words(
        wp_strip_all_tags($content),
        35,
        '...'
    );
}

The code first tries to retrieve the post excerpt. If the post does not have an excerpt, it generates a short description from the article content.

This helps ensure that the schema has a useful description even when you haven’t manually added an excerpt.

4. Retrieve the Author Name

$author_name = get_the_author_meta(
    'display_name',
    $post->post_author
);

This retrieves the display name of the WordPress user who published the post.

If you want to customize how author names appear on your website, see our guide on changing the author display name in WordPress programmatically.

$featured_image = get_the_post_thumbnail_url(
    $post->ID,
    'full'
);

This function retrieves the URL of the post’s featured image.

The snippet adds the image property only when a featured image exists. This avoids generating an empty image value for posts without a featured image.

6. Add Publication and Modification Dates

'datePublished' => get_post_time('c', false, $post),

'dateModified' => get_post_modified_time('c', false, $post),

These functions retrieve the original publication date and the last modified date of the post.

The c format returns an ISO 8601 date, which is suitable for structured data.

7. Convert the Schema Array into JSON

$json_schema = wp_json_encode(
    $schema,
    JSON_UNESCAPED_SLASHES |
    JSON_UNESCAPED_UNICODE |
    JSON_HEX_TAG |
    JSON_HEX_AMP |
    JSON_HEX_APOS |
    JSON_HEX_QUOT
);

WordPress provides wp_json_encode() to convert a PHP array into JSON. This is safer and more reliable than manually building a JSON string.

8. Output the Schema in the Head Section

add_action('wp_head', function () {
    // Schema generation code.
});

The wp_head action outputs the JSON-LD script in the document head. Search engines can read the structured data without any visible changes to the article.

Can You Add More Properties to BlogPosting Schema?

Yes. You can extend the schema with additional properties when the information is available and accurately describes your article.

For example, you can add the article’s word count:

'wordCount' => str_word_count(
    wp_strip_all_tags($post->post_content)
),

You can also add the article category as its section name:

$categories = get_the_category($post->ID);

if (!empty($categories)) {
    $schema['articleSection'] = $categories[0]->name;
}

Another option is to retrieve custom information from Advanced Custom Fields (ACF), provided the field contains relevant and accurate data. You can learn more about working with ACF in our tutorial on customizing ACF repeater fields.

Remember that additional properties should describe the actual article. Avoid adding empty or misleading values just to make the schema larger.

Important: Avoid Duplicate Schema Markup

Before adding this code, check whether your WordPress theme or SEO plugin already generates Article or BlogPosting schema.

Many SEO plugins automatically add structured data to blog posts. If you add another implementation without checking, your page may contain duplicate or conflicting Article markup.

If your existing SEO plugin already generates valid Article schema, you usually do not need this additional snippet. Use one consistent implementation and make sure its information matches the visible article.

How to Test BlogPosting Schema in WordPress

After adding the PHP code, test a published blog post to confirm that the JSON-LD markup is present and valid.

Step 1: Open an Individual Blog Post

Visit any published blog post on your WordPress website.

Step 2: View the Page Source

Right-click the page and select View Page Source. Then search for the following text:

application/ld+json

You should find a script containing your BlogPosting JSON-LD data.

Step 3: Use Google’s Rich Results Test

Open the Google Rich Results Test and enter your blog post URL.

The tool will check the structured data Google can detect and report relevant errors or warnings for supported rich result types.

Step 4: Validate the Schema Markup

You can also use the Schema.org Validator to inspect your structured data and check whether the properties follow the Schema.org vocabulary.

Common Mistakes to Avoid

  • Adding duplicate schema: Check whether your theme or SEO plugin already generates Article markup.
  • Using the wrong conditional: Use is_singular('post') when you only want schema on standard blog posts.
  • Missing featured images: Do not output an empty image property when a post has no featured image.
  • Incorrect dates: Use the actual WordPress publication and modification dates.
  • Invalid JSON: Use wp_json_encode() instead of manually creating a JSON string.
  • Incorrect author information: Make sure the schema author matches the actual article author.
  • Misleading descriptions: Use a relevant excerpt or a description generated from the article content.

Frequently Asked Questions

Do I need a plugin to add BlogPosting schema in WordPress?

No. You can generate BlogPosting schema using PHP and built-in WordPress functions. However, if your SEO plugin already generates valid Article schema, you generally do not need to add a second implementation.

Will this code work for all WordPress blog posts?

Yes. The code runs automatically on individual standard blog posts because it uses is_singular('post'). It does not add BlogPosting schema to regular pages, category archives, or other post types.

Does BlogPosting schema improve Google rankings?

Schema markup helps search engines understand your content, but it does not guarantee higher rankings. It may help your content qualify for supported search enhancements, depending on Google’s requirements and eligibility systems.

Can I add ACF custom fields to BlogPosting schema?

Yes. You can retrieve Advanced Custom Fields values using get_field() and add suitable values to the schema. Make sure the custom field contains accurate information that is relevant to the article.

Where should I add the schema PHP code?

You can add the code to your child theme’s functions.php file or include it in a custom PHP file loaded by your theme.

Can I use BlogPosting schema for WordPress pages?

BlogPosting is intended for blog articles. For regular pages, choose a structured data type that accurately describes the page instead of applying BlogPosting schema to every URL.

Final Thoughts

Adding BlogPosting schema in WordPress without a plugin is straightforward. With a small PHP snippet, you can generate structured data automatically using information already stored in WordPress.

The code in this tutorial adds the article headline, description, author, publication date, modified date, publisher, and featured image when available.

Before publishing the code, check whether your SEO plugin or theme already generates Article schema. Then test your markup to make sure it is valid and does not create duplicate structured data.

Once configured correctly, WordPress can generate BlogPosting schema automatically for your standard blog posts without requiring manual changes to every article.