How to add favicons to your blog comments

  • 0
  • June 16, 2007
Patrick Altoft

Patrick Altoft

Director of Strategy

A couple of readers emailed to ask how we got favicons to show up in the comments section of this blog, hopefully this post will explain the process so you can install favicons on your blog too.

One of the great benefits of using icons on blog comments is that it encourages reader participation and makes readers comments stand out. If you don’t currently have a favicon for your blog you can either create one yourself or borrow a nice 16×16 pixel image from here. If you want to create your own favicon just design an image and reduce it to a 16×16 pixel square and save it as a .ico file. For more details this guide to creating a good favicon is a good read.

Favicon in the comments

We don’t actually run BlogStorm using an off the shelf blog platform such as WordPress, all the code behind this site is custom written, which makes adding details such as favicons very simple – if you are a php coder.

Favatars WordPress Plugin

Most of you probably run WordPress and my intention was to write a nice plugin for you to download but luckily somebody has already released one. Download the Favatars plugin and you can have favicons on your comments in minutes.

The Code

If, like me, you don’t run WordPress you will probably need to use some custom php code to add favicons to your comments.

The first step will be to add an extra column to your comments database, I called my column “favicon”.

Next you need to add the following php code to the file that processes your comments and adds them to the database when they are submittted.

$url=$_GET["url"];

$url = str_replace(“http://”, “”, $url);

$url = str_replace(“www.”, “”, $url);
if (@fclose(@fopen(“http://$url/favicon.ico”,
“r”))) {

$exists = “http://$url/favicon.ico”;

} else {

$exists = “http://www.blogstorm.co.uk/
images/comment.gif”;

}

$SQLcommand = “INSERT INTO comments “;

$SQLcommand = “$SQLcommand (username,usercomment,userurl,
useremail,post_id,favicon) “;

$SQLcommand = “$SQLcommand VALUES(“;

$SQLcommand = “$SQLcommand ‘$username’,’$usercomment’,
‘$userurl’,’$useremail’,’$post_id’,’$exists’) “;

MYSQL_QUERY($SQLcommand);

 

The script cleans up the url and then checks whether the file http://www.website.com/favicon.ico exists. If it does then the favicon url is stored in the database. If it doesn’t exist then the url of an alternative 16×16 pixel image is stored instead.

If you have any questions or just want to see your favicon please comment below.

Free of charge. Unsubscribe anytime.