Star Snippets Logo

snippets.com

Easy to use rich snippets

Integration


General

The Star-Snippets Project consists of 3 different components. These are described below. To ensure the complete functionality (star ranking + rich snippets availability ) all three components have to be integrated.

HTML Snippet

The HTML snippet serves as a placeholder on the page for the actual star ranking to be displayed later. Furthermore the styling of the stars can be adapted using configuration parameters . The easiest way to display a rich snippet ranking on the page is to integrate the below snippet.

<body>
  ...
  <star-snippet></star-snippet>
  ...
</body>

Javascript Code

The java script code is used to find the HTML snippets on the page, to evaluate its configuration parameters and to finally display the stars on the right position. We recommend to paste the java script code directly before the closing body tag (</body>) to ensure optimal page loading time.

As the customizing possibilities page explains, it is sufficient to just integrate the HTML snippet together with the following java script code to display one or more star ratings on your website. So visitors will be able to view ratings of others and to rate the website themselves.

<body>
  ...
  <!-- star-snippets.com -->
  <script>
    (function() {
      function async_load(){			
        var s = document.createElement('script');
        s.type = 'text/javascript';
        s.async = true;
        s.src = 'http://star-snippets.com/script';
        //s.src = 'http://star-snippets.com/script?prevent_cache=' + new Date().getTime(); //prevent caching      
        var x = document.getElementsByTagName('script')[0];
        x.parentNode.insertBefore(s, x);
      }
      if (window.attachEvent)
        window.attachEvent('onload', async_load);
      else
        window.addEventListener('load', async_load, false);
    })();
  </script>
</body> <!-- integrate directly before the closing body tag -->

PHP Code (Java, etc.)

The third and last component generates rich snippets based on the visitor rating, to enable search engines like Google to display the ratings already in the search result list. Well rated websites will appear more prominent in search result list and so potentially attract more users.

In principle it is possible to reload rich snippet tags via Ajax. However, search engine crawlers would ignore reloaded snippets. To prevent this, we recommend to request the current rating from star-snippets.com already during website load. This can be done using for example PHP. Star-snippets.com will response and package the current rating into rich snippet tags. These tags can be included into HTML output so that search engine crawlers will be able to access the them.

<body>
  ...
  <?php
    //The url Parameter should match the href Parameter of the javascript code    
    $url = "http://star-snippets.com/rating?url=http://www.example.com";
			
    $title = "Example-Page";
			
    $description = "a description of your example page";
    
    if($title != "")
    {
      $url .= "&name=".rawurlencode($title);
    }
    
    if($description != "")
    {
      $url .= "&description=".rawurlencode($description);
    }
    
    $timeout = 10;
    
    try {
    
      $streamContext = stream_context_create(array('http' => array('timeout' => $timeout)));
      
      $response = file_get_contents($url, $streamContext);

      if (!$response)
      {}
      else
      {
        echo $response;
      }
    } catch (Exception $ex) {
    }
			
  ?>
  ...
</body>

Warning

If the above script generates an error or a warning because function 'file_get_contents' is not available at your hosted, please use the following script instead.

// Comment this line
// $response = file_get_contents($url, $streamContext);

// use following lines instead
$session = curl_init($url);
curl_setopt($session, CURLOPT_HEADER, false);  
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_TIMEOUT, $timeout);
$response = curl_exec($session);
curl_close($session);
  

Joomla (1.5, 1.6, 2.5, 3.0)

The Joomla integration is even easier than the standard because less configuration is necessary. The integration can be done by using a Joomla Plugin. Just click the below button to download the rich snippet plugin.

Download Rich Snippet Plugin

Alternatively, you can do the rich snippet integration also manually as described below. This provides more flexibility but causes little more efforts.

HTML Snippet

The HTML snippet will be integrated at any desired position (e.g. article, banner, module, … ).

<body>
  ...
  <star-snippet></star-snippet>
  ...
</body>

Javascript and PHP Code

The following code can be integrated directly before the closing body tag into the desired Joomla template (index.php).

<body>
  ...
  
  <?php
    
    $doc = JFactory::getDocument();
    
    $url = "http://star-snippets.com/rating?url=".JURI::current();
    
    if($doc->getTitle() != "")
    {
      $url .= "&name=".rawurlencode($doc->getTitle());
    }

    if($doc->getDescription() != "")
    {
      $url .= "&description=".rawurlencode($doc->getDescription());
    }
    
    $timeout = 10;
    
    try {
      $streamContext = stream_context_create(array('http' => array('timeout' => $timeout)));
      
      $response = file_get_contents($url, $streamContext);

      if (!$response)
      {}
      else
      {
        echo $response;
      }
    } catch (Exception $ex) {
    }
			
  ?>

  <!-- star-snippets.com -->
  <script>
    (function() {
      function async_load(){			
        var s = document.createElement('script');
        s.type = 'text/javascript';
        s.async = true;
        s.src = 'http://star-snippets.com/script';
        //s.src = 'http://star-snippets.com/script?prevent_cache=' + new Date().getTime(); //prevent caching      
        var x = document.getElementsByTagName('script')[0];
        x.parentNode.insertBefore(s, x);
      }
      if (window.attachEvent)
        window.attachEvent('onload', async_load);
      else
        window.addEventListener('load', async_load, false);
    })();
  </script>
</body>

Warning

If the above script generates an error or a warning because function 'file_get_contents' is not available at your hosted, please use the following script instead.

// Comment this line
// $response = file_get_contents($url, $streamContext);

// use following lines instead
$session = curl_init($url);
curl_setopt($session, CURLOPT_HEADER, false);  
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_TIMEOUT, $timeout);
$response = curl_exec($session);
curl_close($session);
  

WordPress

Also the blog software WordPress provides a plugin to easily integrate rich snippets. Just click the following button to download the rich snippet plugin.

Download rich snippet plugin