Facebook comments & SEO
Facebook comments has recently emerged as replaced for comment boxes on regular websites. There are many advantages - such as spam control, posting comments as yahoo, Facebook or other users, ability to post on Facebook profile's wall etc.
We started using the Facebook comments on most of our community based websites. But we always wanted to find a way to get the SEO benefits from comments posted on website.
A blog by Sean Percival shows how to use make use of Facebook comments and make them SEO friendly. I have taken the code and modified so that it can used on Drupal based websites.
<div style="visibility:hidden">
<?php
/* PLEASE DON'T MODIFY THE CODE BELOW THIS COMMENT */
class SEO_FBComments {
const GRAPH_COMMENTS_URL = "https://graph.facebook.com/comments/?ids=";
private $pattern;
private $pageUrl;
/**
* @param string $pattern
* @param bool $debug
*/
public function __construct($pattern = null) {
$this->pageUrl = $this->getSelfUrl();
$this->pattern = $this->getPattern($pattern);
$this->echoComments();
}
function echoComments() {
$oldTimezone = ini_get("date.timezone");
ini_set("date.timezone", "UTC");
$comments = $this->GetFBCommentsHTML($this->pageUrl, $this->pattern);
$comments = "$comments";
ini_set("date.timezone", $oldTimezone);
echo $comments;
}
function getPattern($pattern) {
global $STD_PATTERN;
if(is_null($pattern)) $pattern = $_REQUEST["pattern"];
if(!$pattern) $pattern = $STD_PATTERN;
return $pattern;
}
/**
*
* Retrieves a list of Facebook comments
* from the Comments Plugin
*
* @param string $ids
* @return array
*/
function GetFBComments($ids) {
$url = self::GRAPH_COMMENTS_URL . $ids;
$content = file_get_contents($url);
$comments = json_decode($content);
$comments = $comments->$ids->data;
return $comments;
}
function dayDiff($date1, $date2 = null) {
if(is_null($date2)) $date2 = time();
$dateDiff = abs($date1 - $date2);
$fullDays = floor($dateDiff / (60 * 60 * 24));
return $fullDays;
}
function formatDate($date) {
$dateFormat = "F j \a\\t g:ia";
$date = strtotime($date);
$daysBefore = $this->dayDiff($date);
if($daysBefore > 6)
$formatteddate = date($dateFormat, $date);
else {
switch ($daysBefore) {
case 0:
$day = "Today";
break;
case 1:
$day = "Yesterday";
break;
default:
$day = date("l", $date);
break;
}
$formatteddate = "$day at " . date("g:ia", $date);
}
return $formatteddate;
}
function getComment($data) {
$username = $data->from->name;
$userid = $data->from->id;
$messageid = $data->id;
$message = $data->message;
$date = $data->created_time;
$formatteddate = $this->formatDate($date);
$USER = json_decode(file_get_contents("https://graph.facebook.com/$userid"));
$userpicture = "https://graph.facebook.com/$userid/picture";
$userlink = "http://www.facebook.com/" . $USER->username;
$comment = "$username $message $formatteddate";
return $comment;
}
function getComments($comments) {
$html = "";
foreach ($comments as $data) {
$item = $this->getComment($data);
$html .= $item;
if($data->comments)
$html .= $this->getComments($data->comments->data);
}
return $html;
}
function getSelfUrl() {
$protocol = ($_SERVER["SERVER_PORT"] == "80") ? "http" : "https";
return "$protocol://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
}
function GetFBCommentsHTML($ids, $pattern) {
$comments = $this->getFBComments($ids);
$html = $this->getComments($comments);
return $html;
}
}
new SEO_FBComments;
?>
</div>
Hope this script helps you in gain SEO score.
- vinayras's blog
- 11962 reads