Skip to content

Instantly share code, notes, and snippets.

@cmandesign
Last active June 3, 2020 13:59
Show Gist options
  • Save cmandesign/aa7811493e213a91fff5ba46eeec4069 to your computer and use it in GitHub Desktop.
Save cmandesign/aa7811493e213a91fff5ba46eeec4069 to your computer and use it in GitHub Desktop.

How to Add Rich Snippet to Wordpress Themes Manually Using Yoast + WP-PostRatings

In this piece we will learn how we can easily add rich snippets to our Wordpress theme manually without headache or using complex plugins.

Requirements

  • You should be familiar with Wordpress & Yoast SEO
  • A little knowledge of PHP to understand and make few edits in my codes based on your requirement
  • Patience to read this article carefully, you should know that I am lazy as you are and I don't want to be talkative! I'm just writing the important things to make sure you know what we are doing here.

Story

As you know, Google started taking Rich Snippets into consideration and Yoast only added Schema support for Wordpress 5.0+ new editor and it's worth noting that rich snippets have higher priority compared to old meta keywords so your configuration for Yoast meta keywords will be useless in search results if you use another plugin for adding rich snippets in WordPress.

Also, there is a popular plugin called Post Ratings that is very cool and has more than 100K downloads from the WordPress repository and has a built-in support for the rich snippet. That is very useful but as we know, it will override Yoast meta keywords and your search results view won't be the template that you have set in the Yoast appearance section.

Now we know what the story is, but what is my solution?

My Solution

I considered adding rich snippet data manually to WordPress theme since I developed the theme myself. So I started to edit single.php :

1. Find the WordPress loop

It should be something like this

https://gist.github.com/a91a449cb7ecaea6692c548768965c89

2. Prepare Your JSON-LD Format, Based on your website

Before you do anything, I should tell you that your content subject and type are really important in the snippet that you want to generate. For example, if your content is video you should use video format or if your website is a blog or you are writing news or article you should use Article/ArticleNews format. So please check out following links to know what format you should use and you can easily prepare and develop your format at the end of this article.

JSONLD.com (Article) : please check other formats

Google Developer Section ( with more details )

My website's contents are articles, so here is what I want to generate

https://gist.github.com/9cb2c13ce2da38b714fa01858ac4bfa2

3.Generate the JSON-LD format

Read the following code carefully, I wrote notes and explanations inside the box. Afterwards you can add it anywhere inside the Wordpress loop in single posts, pages or any other post types. I prefer to create a new file called 'single_post_rich_snippet.php' and insert the following code into it, then I will include this file anywhere.

https://gist.github.com/75aa728814c79dd1b1da3fabd0807052

Note : if you don't have WP-PostRatings or you don't like to show ratings in google search result, just remove "aggreagateRating" key and it's value from end of script.

Here is the final result : https://gist.github.com/13390e96ff95f886583efe616c72a006

Almost done! We should just validate our codes before moving on.

Validating the Result

For validation of our codes, we could use the Structured Data Testing Tool https://search.google.com/structured-data/testing-tool

And now we are able to enter our webpage URL and see the result, warnings, and errors. If everything looks good, we have to wait in the Google Search Console to see the Google crawlers results :)

Yoast Breadcrumb as Rich Snippet

I suggest you use Yoast Breadcrumb in your theme too, it's very easy to add and use, plus it makes your search result appear better.

Final Words

Please comment and share your experience with me and help me to improve the solution I came up with.

Authors and Contributors

Thanks to Amir Arzani for proofreading this piece

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment