Last active
December 31, 2016 07:14
-
-
Save alkah3st/a3b525cc94347bc71df40d00e94f7343 to your computer and use it in GitHub Desktop.
Google Structured Data: Add Review Snippets & Become an Instant Critic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Adds Google Schema Data (Posts) | |
*/ | |
function enqueue_review_jsonld() { | |
if (is_singular('post')) : | |
global $post; | |
$post_thumbnail = get_the_post_thumbnail_url( $post); // the featured thumbnail of our review | |
// film data | |
$star_rating = get_field('star_rating', $post->ID); | |
$film_title = get_field('film_title', $post->ID); | |
$film_url = get_field('film_url', $post->ID); | |
$film_release = get_field('film_release_date', $post->ID); | |
$film_cover = (get_field('film_cover', $post->ID) ? get_image(get_field('film_cover', $post->ID) : $post_thumbnail); // if we have no film cover, default to the post's thumbnail | |
// director data | |
$director = get_field('film_director', $post->ID); | |
$director_url = get_field('film_director_url', $post->ID); | |
// actor data | |
$actor = get_field('film_actor', $post->ID); | |
$actor_url = get_field('film_actor_url', $post->ID); | |
if ($star_rating) : ?> | |
<script type="application/ld+json"> | |
{ | |
"@context": "http://schema.org/", | |
"@type": "Review", | |
"author": { | |
"@type": "Person", | |
"name": "Daniel Quinn", | |
"sameAs":"https://plus.google.com/107799649011330304604" | |
}, | |
"url": "<?php echo get_permalink($post->ID); ?>", | |
"datePublished":"<?php echo get_the_time('c', $post); ?>", | |
"publisher": { | |
"@type":"Organization", | |
"name":"DQuinn.net", | |
"sameAs":"<?php echo get_bloginfo('url'); ?>" | |
}, | |
"description":"<?php echo esc_attr(strip_tags(get_the_excerpt())); ?>", | |
"inLanguage":"en", | |
"itemReviewed": { | |
"@type": "Movie", | |
"name": "<?php echo $film_title; ?>", | |
"dateCreated":"<?php echo $film_release; ?>", | |
"director" : { | |
"@type" : "Person", | |
"name": "<?php echo $film_director; ?>", | |
"sameAs": "<?php echo $film_director_url; ?>" | |
}, | |
"actor" : { | |
"@type" : "Person", | |
"name": "<?php echo $film_actor; ?>", | |
"sameAs": "<?php echo $film_actor_url; ?>" | |
}, | |
"sameAs": "<?php echo $film_url; ?>", | |
"image": { | |
"@type": "ImageObject", | |
"url": "<?php echo $film_cover; ?>", | |
"height": 480, | |
"width": 850 | |
} | |
}, | |
"reviewRating": { | |
"@type": "Rating", | |
"worstRating": 1, | |
"bestRating": 5, | |
"ratingValue": <?php echo $star_rating; ?> | |
}, | |
"image": { | |
"@type": "ImageObject", | |
"url": "<?php echo $post_thumbnail; ?>", | |
"height": 480, | |
"width": 850 | |
} | |
} | |
</script> | |
<?php endif; | |
endif; | |
} | |
add_action( 'wp_head','enqueue_review_jsonld' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php $star_rating = get_field('star_rating'); ?> | |
<?php if ($star_rating) : ?> | |
<div class="rating-wrap"> | |
<fieldset class="rating"> | |
<input type="radio" id="star5" name="rating" value="5" /><label class="full<?php if ($star_rating == 5): ?> active<?php endif; ?>" for="star5"></label> | |
<input type="radio" id="star4half" name="rating" value="4 and a half" /><label class="half<?php if ($star_rating == 4.5): ?> active<?php endif; ?>" for="star4half"></label> | |
<input type="radio" id="star4" name="rating" value="4" /><label class="full<?php if ($star_rating == 4): ?> active<?php endif; ?>" for="star4"></label> | |
<input type="radio" id="star3half" name="rating" value="3 and a half" /><label class="half<?php if ($star_rating == 3.5): ?> active<?php endif; ?>" for="star3half"></label> | |
<input type="radio" id="star3" name="rating" value="3" /><label class="full<?php if ($star_rating == 3): ?> active<?php endif; ?>" for="star3"></label> | |
<input type="radio" id="star2half" name="rating" value="2 and a half" /><label class="half<?php if ($star_rating == 2.5): ?> active<?php endif; ?>" for="star2half"></label> | |
<input type="radio" id="star2" name="rating" value="2" /><label class="full<?php if ($star_rating == 2): ?> active<?php endif; ?>" for="star2"></label> | |
<input type="radio" id="star1half" name="rating" value="1 and a half" /><label class="half<?php if ($star_rating == 1.5): ?> active<?php endif; ?>" for="star1half"></label> | |
<input type="radio" id="star1" name="rating" value="1" /><label class="full<?php if ($star_rating == 1): ?> active<?php endif; ?>" for="star1"></label> | |
</fieldset> | |
</div> | |
<?php endif; ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* star ratings | |
*/ | |
.rating-wrap { | |
overflow: hidden; | |
} | |
.rating { | |
border: none; | |
padding: 0; | |
margin-bottom: 20px; | |
margin-left: 0; | |
float: left; | |
clear: right; | |
} | |
.rating > input { display: none; } | |
.rating > label:before { | |
@include font-size(24px); | |
margin: 5px; | |
font-family: "fontello"; | |
display: inline-block; | |
content: '\e819'; | |
} | |
.rating > .half:before { | |
content: '\e81b'; | |
position: absolute; | |
} | |
.rating > label { | |
color: $gray-silver; | |
float: right; | |
} | |
// active state | |
.rating:not(:checked) > label.active, | |
.rating:not(:checked) > label.active ~ label { color: $orange; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment