So I am finally getting the hang of spending some spare time on my website. It's not like I've had a sea of spare time, but we'll get there. I am satisfied with how the website is progressing, slowly but steady - just applying some tricks and tweaks right now.
Facebook's Social Plugins seem to be working fine! I have attached a bit of code to it and now I get notified of new comments by mail. I am still puzzling with the HTML/CSS of these posts; I'm trying to keep it as simple as possible - yet it has to be decent in both looks and function. I will note the code for the plugin and how it's been applied here, for those who are interested.
Plugin Code
So ofcourse we start with the meta tags in the header, like here:
<meta property='fb:admins' content='your_facebook_id' />
After that, we place the following script where you want the Facebook comments to appear.
<script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>
<fb:comments notify="true" href="<?php the_permalink(); ?>" width="640"></fb:comments></div>
In this piece of code the href is the permalink WordPress provides us with, but change this to your needs. Change the width to the width that fits your design and your plugin should already be working just fine. But read further if you are like me, and would love to be notified when there are new comments. Because that's also possible.
Facebook Comment Notification
In order to receive these notification you will need to subscribe to the comment.create event. I won't even link you to Facebook's documentation around it, as that is mostly just pure gibberish. But I will show you the code and explain it.
FB.Event.subscribe('comment.create', function(response) {
$.post('fb_notify.php', {
"title": "<?php the_title(); ?>",
"action": "commentcreation",
"url_of_page_comment": response.href,
"id_of_comment_object": response.commentID
}); });
</script>
When adding this to the body, you basically create an event listener that will post the title of the WordPress post, the page that was commented on and the comment ID to fb_notify.php. On top of that we send the action to the php file, so we can prevent triggering it on just loading the file without their actually having been a comment.create event.
Now that you have the page setup, you only need to write the script that sends you the notification - in this case by mail. Create a file called fb_notify.php and insert the following code:
$admin_email = 'you@domain.com';
$title = $_REQUEST['title'];
$action = $_REQUEST['action'];
$commentID = $_REQUEST['id_of_comment_object'];
$page_href = $_REQUEST['url_of_page_comment']
$headers = "From: Danny <noreply@domain.com>\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$message = "Dear Danny,<br /><br />
There's been commented on your post called '$title'.<br />
View the comment <a href='{$page_href}'>right here</a>.<br /><br />
Yours sincerely,<br />
Danny";if ($action == "commentcreation") {
mail($admin_email, "New comment on {$title}", $message, $headers);
} else {
// Do whatever you want it to do when the page is loaded.
}
?>
Of course, you change here whatever needs to be changed. After this you should have a page with Facebook's comments plugin installed; you should be able to moderate the comments and you should be notified when any new comments have been posted.
So yeah! Enjoy your new comment plugin! That was it basically. If I forgot anything or if you have any further questions, just let me know.