Skip to content

Instantly share code, notes, and snippets.

@hellofromtonya
Created April 5, 2017 14:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hellofromtonya/d37e4059a7d4ff5fa42e44452cdeb65e to your computer and use it in GitHub Desktop.
Save hellofromtonya/d37e4059a7d4ff5fa42e44452cdeb65e to your computer and use it in GitHub Desktop.
Change the CreativeWork itemtype microdata - Genesis.
<?php
// ..code removed for brevity.
add_filter( 'genesis_attr_entry', 'change_creative_work_itemtype', 15 );
/**
* Change the CreativeWork itemtype.
*
* @since 1.0.0
*
* @param array $attributes Array of attributes for the "entry" element.
*
* @return array
*/
function change_creative_work_itemtype( array $attributes ) {
if ( ! is_single() ) {
return $attributes;
}
$creative_work_type = get_post_meta( get_the_ID(), 'creative_work_type', true );
if ( is_valid_creative_work_type( $creative_work_type ) ) {
$attributes['itemtype'] = 'http://schema.org/' . esc_attr( $creative_work_type );
}
return $attributes;
}
/**
* Checks if the given metadata is a valid creative work type.
*
* @since 1.0.0
*
* @param string $creative_work_type Creative work type metadata
*
* @return bool
*/
function is_valid_creative_work_type( $creative_work_type ) {
if ( ! $creative_work_type ) {
return false;
}
$valid_creative_work_types = array(
'CreativeWork', 'Book', 'Movie', 'MusicRecording', 'Recipe', 'TVSeries',
);
return in_array( $creative_work_type, $valid_creative_work_types );
}
<?php
/**
* Genesis Framework.
*
* WARNING: This file is part of the core Genesis Framework. DO NOT edit this file under any circumstances.
* Please do all modifications in the form of a child theme.
*
* @package Genesis\Markup
* @author StudioPress
* @license GPL-2.0+
* @link http://my.studiopress.com/themes/genesis/
*/
// code removed for brevity
add_filter( 'genesis_attr_entry', 'genesis_attributes_entry' );
/**
* Add attributes for entry element.
*
* @since 2.0.0
*
* @param array $attributes Existing attributes for entry element.
* @return array Amended attributes for entry element.
*/
function genesis_attributes_entry( $attributes ) {
$attributes['class'] = join( ' ', get_post_class() );
if ( ! is_main_query() && ! genesis_is_blog_template() ) {
return $attributes;
}
$attributes['itemscope'] = true;
$attributes['itemtype'] = 'http://schema.org/CreativeWork';
return $attributes;
}
// code removed for brevity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment