Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save danielpowney/08959388bf0aba42ceb9e1fb1f434f48 to your computer and use it in GitHub Desktop.
Save danielpowney/08959388bf0aba42ceb9e1fb1f434f48 to your computer and use it in GitHub Desktop.
<?php
/**
* Adds Article itemReviewed for aggreagateRatings schema.org microdata
*
* @param $microdata
* @param $post_id
* @return $microdata
*/
function mrp_microdata_aggregate_rating_item_reviewed_article( $microdata, $post_id ) {
$post_obj = get_post( $post_id );
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ) );
$image_content = '';
$image_width = '';
$image_height = '';
if ( isset( $image[0] ) ) {
$image_content = $image[0];
$image_width = $image[1];
$image_height = $image[2];
}
$display_name = get_the_author_meta( 'display_name', $post_obj->post_author );
$microdata = '<span itemprop="itemReviewed" itemscope itemtype="http://schema.org/Article">';
// default required itemprops for "http://schema.org/Article"
$microdata .= '<meta itemprop="datePublished" content="' . date( 'Y-m-d', strtotime( $post_obj->post_date ) ) . '" />';
$microdata .= '<meta itemprop="dateModified" content="' . date( 'Y-m-d', strtotime( $post_obj->post_modified ) ) . '" />';
$microdata .= '<meta itemprop="headline" content="' . $post_obj->post_title . '" />';
$microdata .= '<span itemprop="mainEntityOfPage"><meta itemprop="name" content="' . $post_obj->post_title . '" /></span>';
$microdata .= '<span itemprop="image" itemscope itemtype="https://schema.org/ImageObject">';
$microdata .= '<meta itemprop="url" content="' . $image_content . '" />';
$microdata .= '<meta itemprop="width" content="' . $image_width . '" />';
$microdata .= '<meta itemprop="height" content="' . $image_height . '" />';
$microdata .= '</span>';
// author
$microdata .= '<span itemprop="author" itemscope itemtype="https://schema.org/Person">';
$microdata .= '<meta itemprop="name" content="' . $display_name. '"/>';
$microdata .= '</span>';
$microdata .= '<span itemprop="publisher" itemscope itemtype="https://schema.org/Organization">';
$microdata .= '<meta itemprop="name" content="' . get_bloginfo( 'name' ) . '" />';
$microdata .= '</span>';
$microdata .= '<span itemprop="logo" itemscope itemtype="https://schema.org/ImageObject">';
$microdata .= '<meta itemprop="url" content="' . get_site_icon_url() . '" />';
$microdata .= '</span>';
$microdata .= '</span>';
return $microdata;
}
add_filter( 'mrp_microdata_aggregate_rating_item_reviewed', 'mrp_microdata_aggregate_rating_item_reviewed_article', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment