Skip to content

Instantly share code, notes, and snippets.

@MaruscaGabriel
Last active August 29, 2015 14:14
Show Gist options
  • Save MaruscaGabriel/6c64ad921aa8597c595d to your computer and use it in GitHub Desktop.
Save MaruscaGabriel/6c64ad921aa8597c595d to your computer and use it in GitHub Desktop.
Genesis code for schema.org
<?php
// Remove the schema markup from an element
function mg_schema_empty( $attr ) {
$attr['itemtype'] = '';
$attr['itemprop'] = '';
$attr['itemscope'] = '';
return $attr;
}
// Change the schema type of an element to Product
function mg_schema_product( $attr ) {
$attr['itemtype'] = 'http://schema.org/Product';
$attr['itemprop'] = '';
$attr['itemscope'] = 'itemscope';
return $attr;
}
// Change the schema type of an element to Event
function mg_schema_event( $attr ) {
$attr['itemtype'] = 'http://schema.org/Event';
$attr['itemprop'] = '';
$attr['itemscope'] = 'itemscope';
return $attr;
}
// Change the schema type of an element to Review
// Make sure the itemprop is set to review so this can be used in conjunction with Event or Product
function mg_schema_review( $attr ) {
$attr['itemtype'] = 'http://schema.org/Review';
$attr['itemprop'] = 'review';
$attr['itemscope'] = 'itemscope';
return $attr;
}
// Set the itemprop of an element to description
function mg_itemprop_description( $attr ) {
$attr['itemprop'] = 'description';
return $attr;
}
// Set the itemprop of an element to name
function mg_itemprop_name( $attr ) {
$attr['itemprop'] = 'name';
return $attr;
}
// Remove the rel="author" and change it to itemprop="author" as the Structured Data Testing Tool doesn't understand
// rel="author" in relation to Schema, even though it should according to the spec.
function mg_author_schema( $output ) {
return str_replace( 'rel="author"', 'itemprop="author"', $output );
}
// Add the url itemprop to the URL of the entry
function mg_title_link_schema( $output ) {
return str_replace( 'rel="bookmark"', 'rel="bookmark" itemprop="url"', $output );
}
<?php
add_filter( 'genesis_attr_content', 'mg_schema_empty', 20 );
add_filter( 'genesis_attr_entry', 'mg_schema_event', 20 );
add_filter( 'genesis_attr_entry-title', 'mg_itemprop_name', 20 );
add_filter( 'genesis_attr_entry-content', 'mg_itemprop_description', 20 );
add_filter( 'genesis_post_title_output', 'mg_title_link_schema', 20 );
function mg_post_info( $info ) {
global $post;
$date = esc_html( get_post_meta( $post->ID, 'date', true ) );
$startdate = '<meta itemprop="startDate" content="' . date('c', strtotime( $date ) ).'">' . $date;
$enddate = esc_html( get_post_meta( $post->ID, 'enddate', true ) );
if ( $enddate && $enddate != $date )
$startdate .= ' - <meta itemprop="endDate" content="' . date( 'c', strtotime( $enddate ) ).'">' . $enddate;
$location = esc_html( get_post_meta( $post->ID, 'location', true ) );
if ( $location )
$location = 'at <span itemprop="location" itemscope itemtype="http://schema.org/Place"><span itemprop="name">' . $location . '</span>, ';
$city = esc_html( get_post_meta( $post->ID, 'city', true ) );
$country = esc_html( get_post_meta( $post->ID, 'country', true ) );
if ( $location )
$location .= '<span itemprop="address" itemscope itemtype="http://schema.org/PostalAddress"><span itemprop="addressLocality">' . $city . '</span>, <span itemprop="addressCountry">' . $country . '</span></span>';
else
$location = '<span itemprop="location" itemscope itemtype="http://schema.org/Place"><span itemprop="address" itemscope itemtype="http://schema.org/PostalAddress"><span itemprop="addressLocality">' . $city . '</span>, <span itemprop="addressCountry">' . $country . '</span></span></span>';
return "$startdate $location";
}
add_filter( 'genesis_post_info' , 'mg_post_info' );
genesis();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment