Skip to content

Instantly share code, notes, and snippets.

@jdevalk
Last active December 12, 2021 20:13
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save jdevalk/5665477 to your computer and use it in GitHub Desktop.
Save jdevalk/5665477 to your computer and use it in GitHub Desktop.
Genesis helper code for schema
<?php
add_filter( 'genesis_attr_content', 'yoast_schema_empty', 20 );
add_filter( 'genesis_attr_entry', 'yoast_schema_event', 20 );
add_filter( 'genesis_attr_entry-title', 'yoast_itemprop_name', 20 );
add_filter( 'genesis_attr_entry-content', 'yoast_itemprop_description', 20 );
add_filter( 'genesis_post_title_output', 'yoast_title_link_schema', 20 );
/**
* We'll use the post info output to add more meta data about the event.
*
* @param string $info
* @return string
*/
function yoast_speaking_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' , 'yoast_speaking_post_info' );
genesis();
<?php
// Remove the schema markup from an element
function yoast_schema_empty( $attr ) {
$attr['itemtype'] = '';
$attr['itemprop'] = '';
$attr['itemscope'] = '';
return $attr;
}
// Change the schema type of an element to Product
function yoast_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 yoast_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 yoast_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 yoast_itemprop_description( $attr ) {
$attr['itemprop'] = 'description';
return $attr;
}
// Set the itemprop of an element to name
function yoast_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 yoast_author_schema( $output ) {
return str_replace( 'rel="author"', 'itemprop="author"', $output );
}
// Add the url itemprop to the URL of the entry
function yoast_title_link_schema( $output ) {
return str_replace( 'rel="bookmark"', 'rel="bookmark" itemprop="url"', $output );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment