How to Add Schema.org to Your WordPress Theme
Join the DZone community and get the full member experience.
Join For FreeAll of the major search engines Google, Yahoo and Bing got together to come up with a way webmasters can better define the content on the page to give search engines more information about what they are crawling. Understanding the content of the page allows the search engines to display more information in the search results. Adding more information in the search results makes it easier for users to find what they want can greatly increase your click through rates in the search engines.
As all the major search engines have agreed on the markup, this makes it easier to implement schema.org and get the benefits from all search engines.
Schema.org allows you to define the information on the page there are many different types such as:
- Movies
- Books
- Recipes
- Video
- Audio
- Events
- Organisation
- Person
- Place
- Review
There are lots more types you can define the page to be, here is the full list.
Structured Data Snippets
To see how this affects your search results in Google, you can get more information about rich snippets in Google Webmaster tools.
From this image, you can see how different these snippets appear in the search results.
For events, you can make the event dates appear in the search results. For movies, you can make a rating or reviews appear in the search results. For a music album, you can make the track list appear in the search results.
How to Use Schema.org
To use schema.org you just have to add a bit more markup to your HTML, this is to tell the search engines what the page is about and what specific items of the page are.
Below is an example of defining a movie in your HTML markup. This starts by finding the outer containing element and defining it as the type of content you are explaining by using the attributes itemscope and itemtype.
<div itemscope itemtype="http://schema.org/Movie"> </div>
Each of the elements in the movie can be defined by using the attribute itemprop. To define the name of the movie you just need to add itemprop="name" to the header tag.
<h1 itemprop="name">Avatar</h1>
Inside the movie element you can also define the director, the genre and a link to the trailer of the movie. Here is the full HTML markup to define a movie area.
<div itemscope itemtype="http://schema.org/Movie"> <h1 itemprop="name">Avatar</h1> <div itemprop="director" itemscope itemtype="http://schema.org/Person"> Director: <span itemprop="name">James Cameron</span> (born <span itemprop="birthDate">August 16, 1954)</span> </div> <span itemprop="genre">Science fiction</span> <a href="../movies/avatar-theatrical-trailer.html" itemprop="trailer">Trailer</a> </div>
What To Use In WordPress
To use schema.org in WordPress, you need to change your theme with the markup required to define the content of your site. With WordPress this can easily be done, as we know the content types of each template file that will be used.
In the HTML tag of your theme, you should add a new function to output the item type of the page.
<html <?php html_tag_schema(); ?> <?php language_attributes(); ?>>
In your functions.php file, add the following function to automatically define the schema type you want to use for the type of content.
function html_tag_schema() { $schema = 'http://schema.org/'; // Is single post if(is_single()) { $type = "Article"; } // Contact form page ID else if( is_page(1) ) { $type = 'ContactPage'; } // Is author page elseif( is_author() ) { $type = 'ProfilePage'; } // Is search results page elseif( is_search() ) { $type = 'SearchResultsPage'; } // Is of movie post type elseif(is_singular('movies')) { $type = 'Movie'; } // Is of book post type elseif(is_singular('books')) { $type = 'Book'; } else { $type = 'WebPage'; } echo 'itemscope="itemscope" itemtype="' . $schema . $type . '"'; }
Because the title tag is normally the same as the headline of the page, you can add the name itemprop to the HTML title tag.
<title itemprop="name"><?php wp_title(''); ?></title>
To define the main content area of your site, find the div that wraps around the the_content() function and add the itemprop mainContentOfPage around the content area.
<div id="content" itemprop="mainContentOfPage"> <?php the_content();?> </div>
To let the search engines know when an article was published or modified, you can use schema.org to define these two settings. On most articles, you will define when the article was published, add an itemprop of datePublished to the date element.
<time class="entry-date" datetime="<?php the_date('F jS, Y'); ?>" itemprop="datePublished" pubdate><?php the_date('F jS, Y'); ?></time>
If you are using custom post types for movies or books then you can can define the different itemprops in the custom post type template files.
All this will allow you to tell the search engines exactly what part of the page is the specific content you want to highlight. Allowing search engines to know who the author was, when the page was changed, the main content for the page and much more.
Published at DZone with permission of Paul Underwood, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments