Integrating Google Rich Snippets Into a WordPress Theme

We're all familiar with the way Google presents search results – with a page title and a little snippet of text for each result. With Google Rich Snippets we can add useful information to the web search result snippet to make it stand out from other results and ultimately attract more visitors. While there are already plugins that provide this kind of functionality in WordPress, there are situations when relaying on a third party plugin is not advisable. In this tutorial, we are going to integrate the microdata format into WordPress theme markup to display a culinary recipe, and make it compatible with Google Rich Snippets' requirements.


Introduction to Google Rich Snippets

Let's take a look at an example of a rich snippet:

I've highlighted for you the snippets of additional information that Google "reads" from the page. As you can see, rich snippets add some really useful information to search engine results. In case of recipes that information includes a photo, recipe rating, calories and total time it takes to prepare the dish. All this additional information gives users much better sense of the content on the page, and makes it more likely that users will click on the link and visit your website.

The snippets for each content type look slightly different and provide information relevant to the specific content type.

How to Enable Rich Snippets?

The secret behind rich snippets is structured, semantic markup, that allows Google to understand the page's content. So basically, all you have to do is properly markup your content to describe the particular type of information on your website. In this tutorial we will be focusing on a culinary recipe, but Google supports rich snippets for a number of other content types, namely:

  • Reviews
  • People
  • Products
  • Businesses and Organizations
  • Events
  • Music

For more information on rich snippets and content types, visit the Google Help Center.

When it comes to marking up your content, there are three markup formats to choose from:

  • microdata
  • microformats
  • RDFa

In this tutorial, we'll be integrating microdata markup with schema.org properties, as recommended in Google rich snippets' documentation. It's worth noting, that the schema.org vocabulary is recognized not only by Google, but also by other major search providers – Yahoo! and Microsoft.

Visit Schema.org for more information and examples on how to implement it in your code.


Step 1 Creating Recipe Custom Post Type

Since we will be writing quite a lot of code, we'll create a separate file called recipe-config.php, to hold all our snippets, and include it using the PHP function include. To do that, open the functions.php file in your current theme directory, and paste the following piece of code at the end:

Now create a new file called recipe-config.php. All the code that follows should be added to that file.

Let's start by creating a new Custom Post Type called Recipe.

Now if you go to the admin area, there should be a new option in the menu called "Recipes". Don't add any recipes just yet, because we need to add some custom meta boxes first.


Step 2 Adding Custom Meta Boxes

The Setup

Because we'll need quite a few custom meta boxes of various types to store all the recipe specific data, I'm going to use the free Custom Meta Boxes and Fields for WordPress library to create them. Of course you could use any other script or create meta boxes from scratch, if you prefer.

Wptuts+ has a great tutorial on the topic of Reusable Custom Meta Boxes

First, we need to download the library from GitHub. As the author suggests, we will store all the script files in 'lib/metabox' folder. So start with creating the 'lib' folder in your theme or child theme, then add the 'metabox' folder inside 'lib'. Unpack and upload all downloaded files to '/wp-content/themes/my-theme/lib/metabox'.

Finally, we need to include the file init.php. Normally you'd include it in your functions.php file but we'll do it in our recipe-config.php, since that's where we store all the recipe specific functions.

Once it's done, we can start defining meta boxes.

Defining Meta Boxes

To qualify for Google Rich Snippets, we don't need to provide all properties included in the specification, although each content type has a required minimum. In this tutorial, we are going to incorporate the following properties:

  • name
  • recipeCategory
  • image
  • description
  • ingredients
  • instructions
  • recipeYield
  • prepTime
  • cookTime
  • totalTime
  • datePublished
  • author

Note that we won't have to create custom meta boxes for all the properties. For example, totalTime will be calculated based on prepTime and cookTime.

Let's add some custom meta boxes, shall we?

With this piece of code we've created a meta box called "Culinary Recipe" that will show only on the Recipes post type edit screen.

The actual field definitions are stored as an array in the 'fields' property. Let's take a closer look:

Adding a new field is as easy as copying one of the array elements (shown above), and changing values for 'name', 'id', 'desc' and 'type'. The Custom Metaboxes and Fields library offers a number of predefined field types, and a convenient method to define your own.

To facilitate separate input of hours and minutes for cook and prep time, I defined our own field type called 'number'. I utilized one of HTML5's new input types – number, and created a simple validation function, casting integer type on the value supplied by the user.


Step 3 Displaying the Recipe

Now we are finally ready to write some markup. We could create a separate template file for our custom post type and place the markup directly in that template. Instead, we will be putting all the markup inside a function, and appending it to the post content with the_content() filter. This is important, because there are many plugins that add some kind of content, e.g. social media buttons, to the end of the post. This way we make sure, that all the plugin output is displayed below the recipe.

Let's get over the code. First, we pull the global $post object, which gives us access to various useful information about the post being displayed.

Then we use the conditional tag is_singular() to check if a single post of the type 'my_culinary_recipe' is currently being displayed. This is because we didn't create a separate template for our custom post type and thus WordPress is using the more general single.php template (or index.php if there's no single.php) to display the recipe. Using the if statement we make sure that recipe markup won't be displayed on regular posts.

Finally, we retrieve the recipe data using the get_post_meta() function, and place it inside the markup structured according to the microdata format.

Helper Functions

You might notice that I used some additional functions – mcr_time(), mcr__total_time() and mcr_list_items() to retrieve and prepare the data for display. Let's take a look!

Time related properties (prepTime, cookTime and totalTime) expect values in ISO 8601 duration format. To account for that, both of our time related functions will take a format as a parameter, and prepare output accordingly.

The mcr_time() function prepares output for cook and prep times, it accepts two parameters:

  • $type (required) is the type of time we want to display. Accepts two values – 'prep' (default) or 'cook'
  • $format (optional) – indicates that output should be formatted according to ISO 8601 duration format. Accepts only one value – 'iso'.

The mcr_total_time() function calculates and prepares output for recipe total time. Accepts only one parameter – $format, analogous to the $format parameter in the mcr_time() function.

The last helper function displays lists of items – ingredients or instructions, according to the $type parameter.

Now it's time to add some content. Navigate to the Recipes section in the administration area, and add a recipe. The output might need some styling, but if you view the post, you should see the recipe below regular content.

That's it! The only thing left, is to check if our markup is correct with the rich snippet testing tool from Google.

This is the rich snippet preview generated from our HTML markup:

You can test your markup by supplying either a URL or a code snippet to the testing tool.

Once you've added rich snippets markup, wait for the Google crawler to discover it. When Google notices the new markup, it should start displaying rich snippets for your website in search results. You can also submit a request form, to tell Google about rich snippets on your website, but you should give it some time first.


Conclusion

In this tutorial I showed you how to integrate a microdata format with a schema.org vocabulary to display culinary recipes. This example should serve you as a blueprint that you can use to enable rich snippets for other content types. Have you used Google Rich Snippets for anything in your projects? Let us know in the comments below.

Tags:

Comments

Related Articles