Integrating Facebook Comment on Dynamic PHP Web Pages

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:app_id" content="YOUR_APP_ID" />
<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:

<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=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:

<?PHP
$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.

avatar
Don Caprio is a full time blogger / web designer living in Nigeria and apart from blogging, my other obsession is music. I'm a self-confessed Tupac fan, an iOS lover, an Android user, a Windows 8 power user, a writer with a 5-year old unfinished novel... and the list goes on and on. You can add me on Facebook, follow my updates on Twitter or add me to your circles on ! You can as well grab my feed and get updates through email.
avatar

@doncaprio

Tech blogger at http://doncaprio.com Web designer at http://doncaprio.org
5 Best Antivirus Software For Windows 8 In Year 2013 http://t.co/dCSPom4v7l - 7 hours ago
avatar
avatar

Get free daily updates through email

Comments

  1. Doncaprio, this is a nice tips for website exposure. I love your mp3smoke

  2. avatar Jake Hudson says:

    This got me so confused till I got here. Thanks for this post. Finally got the comment thingy working on a client’s site

  3. Keep this going please, great job!
    paisajes de tenerife recently posted..paisajes de tenerifeMy Profile

  4. Omo u de try abeg. Bigups Don.

  5. thanks , great article

  6. Thanks for this. Facebook comment box is really important but many bloggers overlook this.

  7. Superb! This fixed the problem I was getting working with WordPress!

  8. Thanks so much! I was looking for something to add to my website, a music search engine just like yours.

    It work liked a charm and i have implented this on all my video pages!

    Thanks again!

  9. Hey Don, thanks for this – helped me a LOT!
    Is there any way to show all latest comments wrapped up as one list?

  10. How to make comment box work unique but with restricted amount of url parameters?

  11. Hi Don, thanks for the tutorial, really needed an alternative to my crappy comment box which wasn’t really functioning. Early test shows this works, however is there a way to remove the link beside the comment box? Beside the comment box, there’ll always be a URL corresponding to the link they are at now. Is there a way to hide or remove it so that it wont be visible? Thanks

    • I really don’t think there should be any such link. Can I please take a look at it? Maybe a page you implemented it on?

      • Hi,

        I think I’ve solved the issue. I simply moved one line of PHP coding to the top of the page and the link disappeared. Thanks a lot on this tutorial though.

  12. avatar Vinicius says:

    MAn thank you a lot From Brazil….. Portuguese posts didn t explain nothing about this…. Thank uuuuu

  13. Wow its works :D
    thanks dude

  14. What about implementing the facebook meta tags (meta property=”og:img: content=”your url to image here”), etc., for the send and like buttons on dynamic websites? Making sure that each page shows the image, description that matches the content on each page?

  15. This is very great totorial Thank alot !

  16. avatar Yogesh Sharma
    Twitter:
    says:

    Great post ! it got me the fair bit of stuff which I kept looking for, in the past. :D Thanks !

Trackbacks

  1. [...] learned this trick from Don Caprio’s post here. He also goes into more details on how to set up Facebook app for comment moderation, just in case [...]

Speak Your Mind

*

CommentLuv badge