Paulund
2013-10-21 #wordpress

Add Schema.org To Your WordPress Theme

All 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. By 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. Full Schema.org Types

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 define this 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 the 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('c'); ?>" 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.

Add Image Prop To Thumbnails

If you use the featured image functionality on your WordPress theme then you will know about the function the_post_thumbnail(), which when placed into the loop will display the thumbnail of the post on the page.

<?php the_post_thumbnail( $size, $attr ); ?>

This function takes two parameters the first allows you to define the size of the image and the second allows you to enter new attributes to use on your image. By default there are 4 sizes that you can use in your post thumbnail, medium, large, full or thumbnail size, these can all be defined in the media library settings. Another option is to pass in an array into the size parameter and the image will come out at this size.

the_post_thumbnail();                  // without parameter -> 'post-thumbnail'

the_post_thumbnail('thumbnail');       // Thumbnail (default 150px x 150px max)
the_post_thumbnail('medium');          // Medium resolution (default 300px x 300px max)
the_post_thumbnail('large');           // Large resolution (default 640px x 640px max)
the_post_thumbnail('full');            // Full resolution (original size uploaded)

the_post_thumbnail( array(100,100) );  // Other resolutions

To add schema.org to these images all you have to do is change the second parameter which takes an array of key value pairs to use as the attributes. The attribute we need to pass in is itemprop => image.

<?php the_post_thumbnail('thumbnail', array('itemprop' => 'image')); ?>

The itemprop tells the schema that this is a featuring image on the page. 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.