Add Facebook Comments in PHP Includes

Facebook comments should include URL of each individual page or they won’t work properly. This can be a real pain if you are managing a static website, because you have to generate and paste code for each individual page manually. You could include their code in your normal includes of course but then ALL comments will appear on ALL pages, which is not a solution.

If your pages process PHP or if you can force them to process PHP as I described in this tutorial, you can as well include your Facebook comments in includes and everything will work properly. The code to include (this is for include itself, let’s call it “comments.php”):

$currentlink = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
echo "<fb:comments href='$currentlink' num_posts='2' width='500'?></fb:comments>";

You can edit number of posts and width if necessary.

You should place a trigger on your pages as follows

<?php include 'includes/comments.php'; ?>

where “includes” is the folder that contains your include files (if you actually have it) and “comments.php” is the name of your include where you pasted the code above. Needless to say you could give it a different name ;-). You should paste this trigger to where you want your comments to appear. This of course has to be done manually for each page but once it’s done everything will be automated.

The comment box will display comments only for each individual page. You will be then able to moderate them at Facebook Developers Tools provided that you followed their instructions and set up an app for your Facebook comments moderation.

I 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 you somehow missed it.