HomePHP 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.
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:
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:
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:
https://www.facebook.com/profile.php?id=987654321
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:
<?php //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']; } <html> <head> <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}"> </head> <body> <!--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> </body> </html>
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.