Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- remove_filter('the_content', 'yasr_add_schema');
- add_filter('the_content', 'yasr_add_schema_archives');
- function yasr_add_schema_archives($content) {
- //Add buddypress compatibility
- if (function_exists('bp_is_active')) {
- //Return content only if is page. This will disable schema for all page.
- //If I try to return $content after if (YASR_SNIPPET == 'overall_rating')
- //or (YASR_SNIPPET == 'visitor_rating') $content will have only wp content, losing the buddypress one
- if (is_page()) {
- return $content;
- }
- }
- $script_type = '<script type="application/ld+json">';
- $end_script_type = '</script>';
- $review_choosen = yasr_get_snippet_type();
- $rich_snippet["@context"] = "http://schema.org/";
- //if doesn't exists a filter for yasr_filter_schema_jsonld $review_chosen value is assigned to $filtered_schema...
- $filtered_schema = apply_filters( 'yasr_filter_schema_jsonld', $review_choosen );
- //So check here if $schema != $review_choosen
- if ($filtered_schema !== $review_choosen) {
- return $content . $script_type . $filtered_schema . $end_script_type;
- }
- $author = get_the_author();
- $review_name = get_the_title();
- $date = get_the_date('c');
- $date_modified = get_the_modified_date('c');
- $post_image_url = ''; //avoid undefined
- $logo_image_url = ''; //avoid undefined
- if (defined('YASR_BLOGPOSTING_ORGANIZATION_LOGO')) {
- $logo_image_url = YASR_BLOGPOSTING_ORGANIZATION_LOGO;
- $post_image_url = $logo_image_url; //this will be overwritten if has_post_thumbnail is true
- $logo_image_url_absolute = $_SERVER['DOCUMENT_ROOT'] . parse_url(YASR_BLOGPOSTING_ORGANIZATION_LOGO, PHP_URL_PATH);
- $post_image_size = @getimagesize($logo_image_url_absolute); //the @ should be useless, just to be safe
- $logo_image_size = @getimagesize($logo_image_url_absolute); //the @ should be useless, just to be safe
- }
- else {
- $post_image_size[0] = 0;
- $post_image_size[1] = 0;
- $logo_image_size[0] = 0;
- $logo_image_size[1] = 0;
- }
- //if exists featuread image get the url and overwrite the variable
- if (has_post_thumbnail() ) {
- $post_image_url = wp_get_attachment_url(get_post_thumbnail_id());
- $post_image_url_absolute = $_SERVER['DOCUMENT_ROOT'] . parse_url($post_image_url, PHP_URL_PATH);
- $post_image_size = @getimagesize($post_image_url_absolute); //the @ should be useless, just to be safe
- }
- if ($review_choosen == "Product") {
- $rich_snippet["@type"]="Product";
- }
- elseif ($review_choosen == "Recipe") {
- $rich_snippet["@type"]="Recipe";
- $rich_snippet["image"] = array(
- "@type" => "ImageObject",
- "url" => $post_image_url,
- "width" => $post_image_size[0],
- "height" => $post_image_size[1]
- );
- }
- elseif ($review_choosen == "Place") {
- $rich_snippet["@type"]="LocalBusiness";
- }
- elseif ($review_choosen == "Other") {
- $rich_snippet["@type"] = "BlogPosting";
- $rich_snippet["datePublished"] = $date;
- $rich_snippet["headline"] = $review_name;
- $rich_snippet["mainEntityOfPage"] = array(
- "@type" => "WebPage",
- "@id" => get_permalink()
- );
- $rich_snippet["author"] = array(
- "@type" => "Person",
- "name" => "$author"
- );
- $rich_snippet["publisher"] = array(
- "@type" => "Organization",
- "name" => YASR_BLOGPOSTING_ORGANIZATION_NAME,
- "logo" => array(
- "@type" => "ImageObject",
- "url" => $logo_image_url,
- "width" => $logo_image_size[0],
- "height" => $logo_image_size[1]
- )
- );
- $rich_snippet["dateModified"] = $date_modified;
- $rich_snippet["image"] = array(
- "@type" => "ImageObject",
- "url" => $post_image_url,
- "width" => $post_image_size[0],
- "height" => $post_image_size[1]
- );
- }
- if (YASR_SNIPPET == 'overall_rating') {
- $overall_rating=yasr_get_overall_rating(FALSE, FALSE);
- if($overall_rating && $overall_rating != '-1' && $overall_rating != '0.0') {
- global $post;
- //name
- $rich_snippet["name"] = $review_name;
- $rich_snippet["Review"] = array (
- "@type" => "Review",
- "name" => "$review_name",
- "author" => array(
- "@type" => "Person",
- "name" => "$author"
- ),
- "datePublished" => "$date",
- "reviewRating" => array(
- "@type" => "Rating",
- "ratingValue" => "$overall_rating",
- ),
- );
- } //END id if $overall_rating != '-1'
- else {
- return $content;
- }
- } //end if ($choosen_snippet['snippet'] == 'overall_rating')
- if (YASR_SNIPPET == 'visitor_rating') {
- $visitor_votes = yasr_get_visitor_votes (4336, FALSE);
- if ($visitor_votes) {
- foreach ($visitor_votes as $rating) {
- $visitor_rating['votes_number']=$rating->number_of_votes;
- $visitor_rating['sum']=$rating->sum_votes;
- }
- }
- else {
- return $content;
- }
- if ($visitor_rating['sum'] != 0 && $visitor_rating['votes_number'] != 0) {
- $average_rating = $visitor_rating['sum'] / $visitor_rating['votes_number'];
- $average_rating = round($average_rating, 1);
- //name
- $rich_snippet["name"] = $review_name;
- $rich_snippet["aggregateRating"] = array (
- "@type" => "AggregateRating",
- "ratingValue" => "$average_rating",
- "ratingCount" => $visitor_rating['votes_number'],
- );
- }
- else {
- return $content;
- }
- }
- if ( !is_404() ) {
- return $content . $script_type . json_encode($rich_snippet) . $end_script_type;
- }
- else {
- return $content;
- }
- } //End function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement