Home arrow PHP arrow Page 2 - How to Add Facebook Comment Boxes with PHP

Integrate Facebook Comments Code with Wordpress or PHP - PHP

Facebook comments are a great way to optimize your site for social media and add a level of user-engagement to you site. Quality website comments can increase your website's credibility, as well as its traffic. This tutorial will teach you how to use Facebook's API Connect to integrate comment boxes on your website in a few simple steps, utilizing a little PHP and some elbow grease.

  1. How to Add Facebook Comment Boxes with PHP
  2. Integrate Facebook Comments Code with Wordpress or PHP
  3. Configuring Your Facebook Comment Box
By: Codex-M
Rating: starstarstarstarstar / 2
June 22, 2011

print this article



Now that your application is fully ready, you can integrate the Facebook comments code to your site. You will be the one to decide where you are going to place the Facebook comments. For example, if you are using Wordpress and you would like to put the Facebook comments on each blog post (assuming you have disabled or removed the Wordpress default comment feature), follow the steps below:

  • Log-in to your Wordpress admin panel.
  • Go to Appearance – Editor
  • Under “Templates” click “Single.php”.
  • Locate the end of the post section and paste this code:

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>
<fb:comments href="<?php the_permalink(); ?>"></fb:comments>

The value of href should the canonical post URL. The primary reason is that when someone in Facebook clicks the link, the user will land on the exact post URL in your website where the comment will be found and not in other places of the site.

The PHP function:

<?php the_permalink(); ?>

Will return the exact and canonical post URL where the comments have been made. This is a Wordpress specific function and will not work with other CMS or blogging software.

  • Save the changes to your single.php file.
  • Now open header.php. If you need to moderate comments and receive notifications for all comments on your website, paste this code in the head section just before the </head> closing tag:

<meta property="fb:app_id" content="{YOUR_APPLICATION_ID}"><meta property="fb:admins" content="{YOUR_USER_ID}">

Replace {YOUR_APPLICATION_ID} with your Application ID. And replace {YOUR_USER_ID} with your Facebook User ID. Example:

Application ID: 123456789
Facebook User ID: 987654321

The code will look like:

<meta property="fb:app_id" content="123456789">
<meta property="fb:admins" content="987654321">

To get your user ID, simply mouse over your name in your friends Facebook profile or comment on any discussion board in Facebook, then mouse over your name. In your browser status bar it will appear as:


Where the user-ID is 987654321

Now clear all of your browser cache, history, etc. Open any of your Wordpress posts. You should see the comment box in the bottom of each post:

Take note that the above procedure is using Wordpress. If you are not using Wordpress, (e.g. you are using Blogger, Drupal or a custom made CMS) you need to consult the CMS documentation or a developer. If you are using PHP in your website, you can implement the entire code above using the layout below:

//function to get the current post URL of the page
function current_page_url(){
$page_url   = 'http';     if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on'){
$page_url .= 's';
return $page_url.'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
<title>This is your post where you need to add a Facebook comments</title>
<meta property="fb:app_id" content="{YOUR_APPLICATION_ID}">
<meta property="fb:admins" content="{YOUR_USER_ID}">
<!--This is the start of your post-->
<!--This is the end of your post, below this you will need to insert Facebook comments-->
<!--Inserting Facebook Comments-->
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>
<fb:comments href="<?php current_page_url(); ?>"></fb:comments>

The above script works with any PHP based website. The current_page_url function grabs the current post URL where the comment will be made.

>>> More PHP Articles          >>> More By Codex-M

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: