Facebook comment system websites is a common thing we see these days and to be candid, it’s a great way to engage your readers and keep your website interactive. I recently finished a music search engine but realized the comment system wasn’t that good. I figured out integrating Facebook comment would be a great way to better socialize it. I know all about adding Facebook comment to static web pages and I’ve integrated Facebook comment on Blogger blogs on several occasions but a dynamic PHP web pages? That was the first time and got me confused for a while. I added the comment alright but the problem I faced was that the same comment was appearing on all web pages and we’re talking of loads of pages here. Each search query on the site automatically generate a unique web page and it’s a really dynamic website.
Of course it’s easy to generate the code from Facebook developers page and implement it on your website but the problem is highlighted below. You will have the same comment on all web pages!
The only way to get rid of this problem is by generating the URL for each page automatically within the Facebook comment code. Wondering how I did this?
1. Be sure to first create a Facebook application that’s going to handle the comments.
2. Enter your domain in the space provided and be sure your website ends with a slash as shown below. Enter other details to create the application.
3. Now, at the head section of your website, add the code below:
<meta property="fb:admins" content="{YOUR_PROFILE_ID}"/>
You should replace YOUR_APP_ID with the App ID/API Key of that app you created earlier. You should see this on the application summary page.
You also replace YOUR_PROFILE_ID with your personal profile ID. This lets you moderate the comment. If you already have a facebook username and don’t know how to retrieve your profile ID, check this post.
That’s done, right? Now let’s move on to the next step.
4. Now, just after the opening <body> tag, paste the code below:
<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=YOUR_APP_ID";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
5. This is the last part that’s a bit tricky. Remember we’re implementing the Facebook comment box on a PHP website with dynamic pages, right? The code we’re going to use next is what displays the comment box but it needs a URL to work. If you specify the href parameter as www.yoursite.com, the same comment will be shown on all pages but in this tutorial, we’re going to automatically generate the URL of each page within the Facebook comment box so each dynamic page has unique comments. Where ever you want to comment box to appear in the body of your website, paste this code:
$currentlink = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
echo "<fb:comments href='$currentlink' num_posts='10' width='570'></fb:comments>";
?>
10 represents the number of comments shown and 570 is the comment box width. You can adjust these if you want.
Like I said, this is strictly for PHP based websites. The comment box shown on each page is unique and you can see this on MP3Smoke.com as I had to find a way to make it work and it finally did. đŸ˜‰ The last part of the code whic is originally in HTML format was converted to PHP and $currentlink automatically generates the URL of the current page.