Shoora
10/18/2018 - 5:06 AM

schema markup Wordpress/Woocommerce

schema markup Wordpress/Woocommerce

function site_level_rich_snippets(){

  ?>

  <script type="application/ld+json">
  {
    "@context" : "http://schema.org",
    "@type" : "WebSite", 
    "name" : "Website Name",
    "url" : "https://website-name/",
    "potentialAction" : {
      "@type" : "SearchAction",
      "target" : "https://website-name/?s={search_term}",
      "query-input" : "required name=search_term"
    }                     
  }
  </script>

  <?php 

    if(is_single()){
    $categories = get_the_category();
    
?>
    
    <?php foreach ($categories as $cat): ?>
      $cat_string .= '"' . $cat->name . '"';
    <?php endforeach ?>

   
    
    <script type="application/ld+json">
    {
      "@context": "http://schema.org",
      "@type": "Article",
      "headline": "<?php echo get_the_title(); ?>",
      "image": "<?php echo get_the_post_thumbnail_url(); ?>",
    "keywords": [<?php echo $cat_string; ?>],
      "datePublished": "<?php echo get_the_date(); ?>",
      "dateModified": "<?php echo get_the_date(); ?>",
      "articleSection": "<?php echo $categories[0]->name; ?>",
      "creator": "<?php echo get_the_author(); ?>",
      "author": "<?php echo get_the_author(); ?>",
      "publisher": {
          "@type":"Organization",
           "name":"Website Name",
           "url":"https://website-name/",
           "logo":{
            "@type": "ImageObject",
            "name": "Website Name Logo",
            "url":"https://website-name-logo.png"
          }
         },
      "articleBody": "<?php echo  esc_html(get_the_excerpt()); ?>",
      "mainEntityOfPage": "True"
    }
    </script>


<?php 
    }
// this one is for yotpo // each product have custom fields aggregate_rating_yotpo, rating_count, date_of_update_rating
    if(is_page(12351231)){
        $aggregate_rating = get_field('aggregate_rating_yotpo');

        $rating_count =  get_field('rating_count');
        $date_of_update_rating = get_field('date_of_update_rating');
        $today = date("Y-m-d");

         if(!$date_of_update_rating || strtotime($today) < strtotime('-1 day')){
            $date_of_update_rating = $today;

            $api_key = 'api key';
            global $post;
            $id = $post->ID;
            $request = wp_remote_get('https://api.yotpo.com/products/api-key/yotpo_site_reviews/bottomline');
            

            if( is_wp_error( $request ) ) {
                return false; // Bail early
            }
            $body = wp_remote_retrieve_body( $request );
            $data = json_decode( $body );
            

            update_field('aggregate_rating_yotpo', $data->response->bottomline->average_score); 
            update_field('rating_count', $data->response->bottomline->total_reviews); 
            update_field('date_of_update_rating', $today ); 

            

         } 
        ?>
        <script type="application/ld+json">{  
           "@context":"http://schema.org",
           "@type":"Organization",
           "name":"Website Name",
           "url":"https://website-name/",
           "contactPoint" : [{
               "@type" : "ContactPoint",
               "telephone" : "+61 07 3102 4842",
               "contactType" : "customer service"
             }],
           "sameAs": ["https://www.facebook.com/website-name/",
                "https://www.instagram.com/website-name/",
                "https://www.linkedin.com/company-beta/website-name/",
                "https://plus.google.com/u/0/b/website-name",
                "https://www.youtube.com/channel/website-name",
                "https://www.pinterest.com/website-name/",
                "https://twitter.com/website-name"
               ],
           "aggregateRating":{  
              "@type":"AggregateRating",
              "ratingValue":"<?php  echo $aggregate_rating; ?>",
              "bestRating":"5", 
              "reviewCount":"<?php  echo $rating_count; ?>"
           },
           "logo":"https://website-name/website-name-logo.png"
        }</script>


<?php

    } else {
      ?>
        
        <script type="application/ld+json">{  
           "@context":"http://schema.org",
           "@type":"Organization",
           "name":"Website Name",
           "url":"https://website-name/",
           "contactPoint" : [{
               "@type" : "ContactPoint",
               "telephone" : "+61 07 3102 4842",
               "contactType" : "customer service"
             }],
           "sameAs": ["https://www.facebook.com/website-name/",
                "https://www.instagram.com/website-name/",
                "https://www.linkedin.com/company-beta/website-name/",
                "https://plus.google.com/u/0/b/website-name5",
                "https://www.youtube.com/channel/website-name",
                "https://www.pinterest.com/website-name/",
                "https://twitter.com/website-name"
               ],
           "logo":"https://website-name/website-name-logo.png"
        }</script>

      <?php
    }



}
add_action('wp_footer','site_level_rich_snippets');