How to add schema markup in WordPress

Adding rich snippets to your WordPress site makes your site friendlier towards the crawlers. It is easier for them to understand what your site / current page is about. To improve your search presence, adding schema markup to your site is important.

Since we have not used any plugins in implementing the structured data on our site, we did not cover this in the previous article that went through different SEO plugins for WordPress.

Adding schema markup with plugins

While this is not the method we use, there are multiple plugins available that allow you to add markup on your site. For most people, this is probably the easiest way to include schema markup on your site.

Currently you can use for example the following plugins:

With the plugin approach, you might end up having troubles if your site has multiple post types. In this case it might be easier to do the implementation yourself in the template files.

Custom implementation on page templates

Instead of using plugins, we have included the markup in the page template. While the plugins may provide an easy setup they might end up coming quite clumsy in the future, especially if your site grows into a more complex one with custom post types and other, extended features.

For the basic post (post type post) you want to add the schema markup in the post template. For us this template file is called content-single.php, but more commonly with WordPress the post template will be called single.php.

Adding the markup

For posts we use the NewsArticle schema from which you can see an example in the same place where you can test your markup: Google’s Structured Data Testing Tool.

In order to use the information from the post, you have to include this markup inside the loop. Here is how we have implemented the markup:

<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "NewsArticle",
  "mainEntityOfPage": {
    "@type": "WebPage",
    "@id": "<?php echo get_permalink($post->ID);?>"
  },
  "headline": "<?php echo $post->post_title;?>",
  "image": {
    "@type": "ImageObject",
    "url": "<?php echo $thumb;?>",
    "height": 250,
    "width": 750
  },
  "datePublished": "<?php echo get_the_date(DateTime::ISO8601);?>",
  "dateModified": "<?php echo get_the_modified_date(DateTime::ISO8601);?>",
  "author": {
    "@type": "Person",
    "name": "<?php echo get_the_author_meta('display_name');?>"
  },
   "publisher": {
    "@type": "Organization",
    "name": "<?php echo bloginfo();?>",
    "logo": {
      "@type": "ImageObject",
      "url": "<?php echo get_site_icon_url( $size = 512 );?>"
    }
  },
  "description": "<?php echo substr(strip_tags($post->post_content), 0, 180);?>"
}
</script>

Schema markup types

While we only showed the markup for the basic post type (NewsArticle), there are few other types that can be used as well. The other common ones are products, recipes and reviews. To get more information on how to improve your search presence, visit https://developers.google.com/search/