Creating the Difference

Create dynamic Google Structured Data

Posted on: 26-09-2018 Last modified: 03-07-2021
Categories: HTML, PHP, Snippets Tags: , , ,

How to create a one-time code for all your events:
Past, present, future & with or without bookings.

NOTE: Ticket prices are an Events Manager Pro feature. If you are only using the free plugin, you need to take those parts out of the snippets!

Telling Google about your events is very important. Of course you want Google to display the correct information, so anyone who finds you knows exactly what you are offering.

On the Google Developer website is says that they will want you to provide this info using jSon mark-up. But when you’re using Events Manager (Pro), it’s much better to not! :-P
Why? Json mark-up does not support the extremely handy conditional placeholders Events Manager offers.

For example:
With the conditional placeholders you can tell Google that the event is sold out, or cancelled etc. Also, if the event has passed, Google will not automatically change the structured data. But with the method below, it will be automatically updated (if needed) every time Google crawls your blog. And you won’t have to alter any of the code for that.

Step 1: Copy & Paste in WordPress.

In your WordPress Admin Dashoard, paste this part at the top of Events → Settings → Formatting → Single event page format. (So above your current mark-up.) Don’t worry: this whole section will not output anything to your visitors, so your lay-out remains 100% untouched. :-)
Don’t forget to change [YOUR COMPANY] to your personal info.

By pasting this in the Single Event Page Format section, the code will automatically apply to every event on your blog. The conditional placeholders will automatically output the correct data for that specific event.

If you are not using Events Manager Pro, but the free version, please replace #_ccTicketPrice with a hard-coded value, like: 10.00

[html] {has_location} {/has_location} {no_location} {/no_location} {no_bookings} {/no_bookings} {has_bookings} {bookings_open} {/bookings_open} {bookings_closed} {has_spaces} {/has_spaces} {fully_booked} {/fully_booked} {/bookings_closed} {/has_bookings} [/html]

Step 2: Edit if needed.

As you can see, there are embedded (multi-level) conditional placeholders. By default Events Manager only supports 1 level. To enable multi-level conditional placeholders, add this to your wp-config.php file (“4” is the number of levels you want to enable):

[php] define(‘EM_CONDITIONAL_RECURSIONS’, 4); [/php]

Step 3: Restore original published date.

By default Events Manager alters the WordPress published date to the event date. For Google we will need the actual published date, because that is the date we will start telling Google about our event and also when the tickets become available. So we simply remove the alteration. It does not have real benefits anyway. So, I personally do not understand why the EM Devs even put it in. :-P

Add this to your functions.php:Click here to learn how to safely add your code to WordPress.

[php] function stonehenge_em_restore_original_date() { remove_filter(‘get_the_date’, array(‘EM_Event_Post’,’the_date’),10,3); } add_action(‘init’, ‘stonehenge_em_restore_original_date’); [/php]

Note: With this filter enabled, Google will get strange data. Say you publish the event on 01/01/2018, but it takes place on 31/03/2018. If Google crawls your blog on 01/02/2018, you are actually telling Google: “Hey, here’s a an event I will publish two months from now, but you may read it anyway.”

Step 4: Create custom placeholders.

Events Manager does not provide any placeholders to output the ticket price without the currency formatting, but Google will want that. So, in the end you will need these custom placeholders:
#_ccTicketPrice, #_ccTicketName, #_ccTicketStart & #_ccTicketEnd.

Add this snippet to your functions.php:Click here to learn how to safely add your code to WordPress.
If you are not using Events Manager Pro, but the free version, please delete lines 11 – 20 below.

[php] function stonehenge_em_create_custom_placeholders_for_google( $replace, $EM_Event, $result ) { global $EM_Event; switch($result) { // Get the ticket name. case ‘#_ccTicketName’: $replace = ”; foreach( $EM_Event->get_tickets()->tickets as $EM_Ticket ) { $replace = wp_kses_data($EM_Ticket->ticket_name); } break; // Ticket price incl. tax, without currency symbol case ‘#_ccTicketPrice’: $replace = ‘0.00’; foreach( $EM_Event->get_tickets()->tickets as $EM_Ticket ) { $taxrate = get_option(‘dbem_bookings_tax’); $tax = ( 1 + (100 / $taxrate) ); $price = sprintf(“%.2f”, ($EM_Ticket->ticket_price * $tax)); $replace = number_format( $price, 2 ); } break; // Display creation date of the ticket / event = start of ticket availability case ‘#_ccTicketStart’: $replace = ”; $start = $EM_Event->post_date; $replace = date_i18n( get_option(‘dbem_date_format’), strtotime($start)); break; // Display the date of the event (ticket cut-off date) case ‘#_ccTicketEnd’: $replace = ”; $end = $EM_Event->rsvp_date; $replace = $end; break; } return $replace; } add_filter( ’em_event_output_placeholder’, ‘stonehenge_em_create_custom_placeholders_for_google’, 1, 3 ); [/php]

Step 5: Test your mark-up.

After all that, go to the Google Structured Data Test Tool to test your mark up. :-)

⇐ Previous
Next ⇒