Created
August 13, 2015 10:04
-
-
Save danielpowney/bf6383b38575983c04bb to your computer and use it in GitHub Desktop.
Adds "http://schema.org/Recipe" microdata with aggregateRating. Replaces the default "http://schema.org/Article"
This file contains hidden or 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 | |
| /** | |
| * Adds "http://schema.org/Recipe" microdata with aggregateRating. Replaces the default | |
| * "http://schema.org/Article" | |
| * | |
| * @param unknown $microdata | |
| * @param unknown $rating_form_id | |
| * @param unknown $post_id | |
| * @param unknown $rating_result | |
| * @return string | |
| */ | |
| function mrp_rating_result_recipe_microdata( $microdata, $rating_form_id, $post_id, $rating_result) { | |
| $microdata = '<div itemscope itemtype="' . "http://schema.org/Recipe" . '" style="display: none;">'; | |
| $post_obj = get_post( $post_id ); | |
| $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ) ); | |
| $image_content = ''; | |
| if ( isset( $image[0] ) ) { | |
| $image_content = $image[0]; | |
| } | |
| $user_info = get_userdata( $post_obj->post_author ); | |
| $author = $user_info->display_name; | |
| // typically you would store this kind of data in the post meta table | |
| /* | |
| $prep_time = get_post_meta( $post_id, 'prep_time' ); // e.g. PT15M | |
| $cook_time = get_post_meta( $post_id, 'cook_time' ); // e.g. PT1H | |
| $ingredients = get_post_meta( $post_id, 'ingredients' ); // e.g. array( '3 or 4 ripe bananas, smashed', '1 egg', '3/4 cup of sugar' ); | |
| $instructions = get_post_meta( $post_id, 'instructions' ); // e.g. Preheat the oven to 350 degrees. Mix in the ingredients in a bowl. Add the flour last. Pour the mixture into a loaf pan and bake for one hour. | |
| $nutrition_info = get_post_meta( $post_id, 'nutrition_info' ); // e.g. array( 'calories' => '240 calories', 'fatContent' => '9 grams fat' ); | |
| */ | |
| $prep_time = 'PT15M'; | |
| $cook_time = 'PT1H'; | |
| $ingredients = array( '3 or 4 ripe bananas, smashed', '1 egg', '3/4 cup of sugar' ); | |
| $instructions = 'Preheat the oven to 350 degrees. Mix in the ingredients in a bowl. Add the flour last. Pour the mixture into a loaf pan and bake for one hour.'; | |
| $nutrition_info = array( 'calories' => '240 calories', 'fatContent' => '9 grams fat' ); | |
| // lets construct the microdata | |
| $microdata .= '<meta itemprop="name" content="' . $post_obj->post_title . '" />'; | |
| $microdata .= '<meta itemprop="description" content="' . $post_obj->post_excerpt . '">'; | |
| $microdata .= '<meta itemprop="author" content="' . $author . '" />'; | |
| $microdata .= '<meta itemprop="prepTime" content="' . $prep_time . '">'; | |
| $microdata .= '<meta itemprop="cookTime" content="' . $cook_time . '">'; | |
| $microdata .= '<meta itemprop="datePublished" content="' . date( 'Y-m-d', strtotime( $post_obj->post_date ) ) . '" />'; | |
| $microdata .= '<meta itemprop="image" content="' . $image_content . '" />'; | |
| $microdata .= '<div itemprop="nutrition" itemscope itemtype="http://schema.org/NutritionInformation">'; | |
| foreach ( $nutrition_info as $name => $content ) { | |
| $microdata .= '<meta itemprop="' . $name . '" content="' . $content . '" />'; | |
| } | |
| $microdata .= '</div>'; | |
| foreach ( $ingredients as $ingredient ) { | |
| $microdata .= '<meta itemprop="recipeIngredient" content="' . $ingredient . '" />'; | |
| } | |
| $microdata .= '<meta itemprop="recipeInstructions" content="' . $instructions . '" />'; | |
| // add aggregate rating | |
| $microdata .= '<span itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">' | |
| . '<span itemprop="ratingValue">' . $rating_result['adjusted_star_result'] . '</span>/<span itemprop="bestRating">5</span>' | |
| . '<span itemprop="ratingCount">' . $rating_result['count_entries'] . '</span>' | |
| . '</span>'; | |
| $microdata .= '</div>'; | |
| return $microdata; | |
| } | |
| add_filter( 'mrp_rating_result_microdata', 'mrp_rating_result_recipe_microdata', 10, 4 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment