Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Facebook plugin comments for dynamic products

I installed facebook comments on my website. My website is a dynamic website and pages are like this www.example.com/page?id=54, www.example.com/page?id=67.

If I post a comment in this page: www.site.com/page?id=54, it also appears in www.example.com/page?id=67. The comments are not unique for a page, but appear in every page

i saw the question : Facebook comments, for each page. The answer in this question is that the problem is because of the "?" sign. It seems that "?" sign in the URL make it broken for the Facebook plugin. And I need to change form of URl writing.

Because my website is 7 years old have incoming links to it, I don’t want to change the method of the URl writing .

is there another way to fix it?

like image 280
Alon Steinberg Avatar asked Jan 25 '12 12:01

Alon Steinberg


3 Answers

Firstly, copy comment div and script from Facebook, paste it to your product details page:

<div id="fb-root"></div>
<script>(function (d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=114215202075258";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>

and

<div class="fb-comments" data-href="http://example.com" data-width="470" data-num-posts="3"></div>

Finally, simply add this code:

<script>
    $(".fb-comments").attr("data-href", window.location.href);
</script>
like image 133
Jeyhun Rahimov Avatar answered Oct 17 '22 22:10

Jeyhun Rahimov


If you're using PHP this is the code which will request the URL of the current page and then link Facebook comments to it:

<?PHP    
$url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
echo "<div class='fb-comments' data-href='$url' data-num-posts='10' data-width='470'></div>";
?>

It works if your dynamic content has only one query string (for example ?product=). If it has more query strings for the same page, for example &sort= for sorting options, then it won't work properly as the Facebook comment which would appear on sorting option ascending wouldn't appear on the sorting option descending, for example.

You can solve this part by assigning a base URL for that product and then showing the FB comments for that URL on all dynamic pages with that product. For example, you're requesting FB comments for the page ?product=13&sort=asc&type=34 even if &sort and &type are different on that page.

like image 23
Dan Horvat Avatar answered Oct 17 '22 21:10

Dan Horvat


When inserting the widget to your page you add something similar to the following code:

<div class="fb-comments" data-href="http://example.com" data-num-posts="2" data-width="470"></div>

You need to replace http://example.com each time for the new page, once with ?id=54 and another time with ?id=67 for each relevant page.

like image 1
Peled Roy Avatar answered Oct 17 '22 20:10

Peled Roy