Events Manager: Extend it with rich snippets

Events Manager, the WordPress plugin, doesn’t support rich snippets out of the box. The photography club, of which I’m a member, uses Events Manager to manage all the events and we would like to use rich snippets,so I had to write some code to do this.

Background information

Events Manager uses placeholders which you can use to display the information of the events. I decided to create my own placeholders and not to rewrite the given placeholders. I know that if you already use the plugin you will have to update all the places you want to use the rich snippet placeholders and this could be a pain, but if we would replace the standard placeholders the rich snippets would be everywhere and you don’t have to use the rich snippets everywhere. I only use them on the front-page where I list 5 upcoming events for example, but you could use them on the search page for events.

Setup Events Manager

In order for the rich snippets to show up you need to add the line

<div itemscope itemtype="http://schema.org/Event">

to the event list format header and of course add a closing div tag in the event list format footer,

I’ll be using three new placeholders:

  • #_SCHEMALINK – For the event URL’s
  • #_SCHEMADATE – For the event data
  • #_SCHEMAPLACE – For the location of the event

You can add these placeholders anywhere you use the Events Manager placeholders, so either in the Events Manager administration section, or if you code to call them.

The code

We will use two filters of the Events Manager plugin.

add_filter('em_event_output_placeholder', 'filter_EM_event_output_placeholder', 10, 4);
add_filter('em_location_output_placeholder', 'filter_EM_location_output_placeholder', 10, 4);

I think the names speak for themselves.
You could use just 1 filter, but I decided to keep the same method as the original Events Manager plugin, which differentiate between events and locations.

The code to replace the placeholders.
For the Events.

/**
* Filter non standard Events Manager placeholders.
* The placeholders are related to the events.
*/
function filter_EM_event_output_placeholder($replace, $em, $full_result, $target)
{
    switch ($full_result) {
        case '#_SCHEMALINK':
            $event_link = esc_url($em->get_permalink());
            $replace = '<meta itemprop="url" content="' . $event_link . '">';
            $replace .= '<meta itemprop="name" content="' . esc_attr($em->event_name) . '">';
            break;
        case '#_SCHEMADATE':
            $replace = '<meta itemprop="startDate" content="' . date('c', $em->start) . '">';
            $replace .= '<meta itemprop="endDate" content="' . date('c', $em->end) . '">';
            break;
    }
    return $replace;
}

For the Locations.

/**
* Filter non standard Events Manager placeholders.
* The placeholders are related to the locations
*/
function filter_EM_location_output_placeholder($replace, $em, $full_result, $target)
{
    switch ($full_result) {
        case '#_SCHEMAPLACE':
            $replace = '<span itemprop="location" itemscope itemtype="http://schema.org/EventVenue">';
            $replace .= '<meta itemprop="name" content="' . $em->location_name . '">';
            $replace .= '<span itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">';
            $replace .= '<meta itemprop="streetAddress" content="' . $em->location_address . '">';
            $replace .= '<meta itemprop="addressLocality" content="' . $em->location_town . '">';
            $replace .= '<meta itemprop="addressRegion" content="' . $em->location_state . '">';
            $replace .= '</span></span>';
            break;
    }
    return $replace;
}

And that’s all there is to it.
If you don’t understand the rich snippet layout in the above code I suggest you read the info at Schema.org

This article is filed under the categories Development » WordPress » Code Snippet and has the following tag associated with it: .
  • Thanks for the write-up, am sure this will help some people out, and we’ll take a look at this again when addressing schema markup in our plugin!

  • Thank you so much for this Peter! Very useful.
    However, I wonder if you could help me with adding the event image to the event?
    I added some code to include the event image in the schema, but it doesn’t seem to be working when I check the site in Google’s Structured Data Testing Tool.

    Here’s the code (apologies if this doesn’t display correctly):
    case '#_SCHEMAIMAGE':
    $event_image = wp_get_attachment_image_src( get_post_thumbnail_id($em->ID), 'medium');
    $replace = '';
    break;

    Originally I had just the image URL set as meta data but when I tested that method it didn’t seem to recognise the image either.

    Having ‘itemprop=”image”‘ within the tag seems to be the more correct way of doing it, as far as I have read. And I added in the ‘rel=”image_src”‘ as this seems make the image show when using social media sharing tools.

    Any ideas what I’m doing wrong?

  • Apologies, here is the complete code:
    http://pastebin.com/ZbnVFjFN

  • Peter

    Grace,

    have a look at this code: http://pastebin.com/sGRWz50w

    I’m assuming you made sure that the $event_image[0] actually holds the correct URL to the image. I didn’t really checked that part.

  • Hi Peter,

    Thanks for the prompt reply.
    Yep, the $event_image[0] holds the URL with no issues.

    I originally had the code you’ve just suggested, but when I test the URL at
    http://www.google.com/webmasters/tools/richsnippets
    It doesn’t display an image. I wonder though if this is an issue with Google’s testing tool, rather than the code markup?

    Anyway, it’s not a huge issue, I’ll use your suggested solution for now, and I’ll monitor it once I’ve moved the site live and it’s fully indexed.
    Thanks again!

  • Siddhant

    In which file I have to replace the codes?

    • Peter

      Add the code to a new plugin, or use the themes functions.php

      • Siddhant

        You mean that I need to copy this code into functions.php?

        • Peter

          Yup

  • Siddhant

    That didn’t work out.
    I added the above code to functions.php and then I changed the formatting ( Events > Settings > Formatting ). I replaced the #_EVENTSLINK to #_SCHEMALINK but it didn’t work out!

    • Peter

      Replacing #_EVENTSLINK with #_SCHEMALINK does not result in a link with schemadata.

  • I need remplace this: #_EVENTNAME this is my problem. when i use #_EVENTEXCERPT{10,…}, works well but not well do.

    #_EVENTEXCERPT{10,...}
    
    	
    #_EVENTURL
    
    #_LOCATIONNAME, #_LOCATIONTOWN, #_LOCATIONCOUNTRY

    View: https://www.google.com/webmasters/tools/richsnippets?q=http%3A%2F%2Fwww.indiehoy.com%2Fagenda%2Fjake-bugg-en-buenos-aires-argentina-2014%2F

    • Peter

      I have no idea what you want to do. #_SCHEMALINK is not an URL. It’s meta data