How do I add schema markup to individual WordPress posts without using plugins?

If you want to include the same type of Schema information about every post, you can do this by creating a child theme.

(Instead of editing your theme directly, you create a style.css file that references the parent, and copy just the files you need to modify into that child theme. This way, when you update your parent theme, your customizations aren’t lost.)

Which Schema you want to use, what information you want to include within it, and what theme you’re using will all determine exactly how and where you include the details. Here’s a basic example to add “Article” schema to your Posts.

style.css

/*
Theme Name: Schema Test
Template: twentytwenty
*/

Your style.css file can be extremely basic. The Theme Name will tell WordPress “Hey, here’s a theme the user can activate,” and the Template refers to the parent theme so WP knows which parent to reference. You will need to change the Template to be the folder of whatever theme you’re using. Here, the parent is the “Twenty Twenty” theme, and we refer to it by “twentytwenty” because that is what the folder containing the theme is called. Adjust as needed. Upload this file to /wp-content/themes/schema-test/. Voila, you have a Child Theme you can activate – though you won’t notice a difference since you’re not actually overriding anything yet.

Next you need to identify which parent theme files contain code that you want to change (or add to). Each theme is a bit different, though typically you will find Posts handled in the “single.php” file. So, copy the “single.php” file from the parent theme into your “schema-test” folder and then edit it.

Let’s say your file currently looks like this:

<?php get_header(); ?>
<main>
    <?php if ( have_posts() ) :
        while ( have_posts() ) : the_post();
            the_content();
        endwhile;
    endif; ?>
</main>
<?php get_footer(); ?>

To make this an Article, you would identify whatever HTML element wraps the content. In this case, there’s a <main> element around just the content. (In many themes, there’s also a sidebar, and you’d want to exclude that – so there might be an inner div or something more specific in your theme.)

All you do is add the itemscope itemtype code for your desired Schema:

You now have a Schema Article. Next you’ll need to determine what information is available – such as the title, the author, etc., etc. This is again very theme-specific, because some themes may include the title inside the <main> element, while others may include it inside “header.php”. So, you always have to determine one HTML element that includes all of the data you want to mark up, which sometimes means adding your own HTML element, such as a <div>, which may span multiple files. And, when you’re editing files like “header.php”, keep in mind all content types use that file, so if you only want to mark up your Posts, you’ll need to add conditionals such as

<?php if is_singular('post') {
// add schema
} ?>