Adding a Facebook Like Button to Your DotNetNuke Blog
Aug
14
Written by:
8/14/2010
Someone on the DotNetNuke Forums asked recently how to add a Like Button for Facebook to their blog. They mentioned something that I had also found out – the Windows Live Writer plugin that’s supposed to do it, but doesn’t work. So how can us DotNetNuke users add a Facebook Like Button to our blogs?
What is a ‘Like’ Button?
A Like Button is an interactive button provided by Facebook, which allows you to instantly “like” a page, and reflect this in your Facebook profile for all of your friends to see. These buttons look something like the image below.
Clicking “like” shows that you liked the page, and who and how many other people have. Facebook allows anyone with a web page to be able to add this as an element on their site.
Get the Like Button Code Code
Use the form Facebook provides to generate the base code needed for your button. It allows you to change a few of the options that are available to you, making the button work like you want. When you generate your button, leave the URL blank.
You should end up with something like this:
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fexample.com%2Fpage%2Fto%2Flike&layout=standard&show_faces=true&width=450&action=like&colorscheme=light&height=80"
scrolling="no" frameborder="0"
style="border:none; overflow:hidden; width:450px; height:80px;"
allowTransparency="true"></iframe>
Alter the Code
The default code won’t work for any site or any page though. The URL in the SRC attribute of the resulting IFRAME points to an imaginary page. However, this gets slightly more complicated. In a dynamic site, you need to be able to change the value of the URL on-the-fly, per page load. We need to make room for that.
First, add an ID attribute to the IFRAME. In my example, I named the IFRAME “ifFacebook” in the ID attribute. (If your implementation is going to have this code snippet appear on the page more than once, use a CLASS attribute instead.)
This ID attribute will allow us to manipulate this specific IFRAME element on the page with the greatest of ease.
Next, remove the “href” key/value pair from the URL that’s in the SRC attribute. Add “&href=” to the end of the remaining URL. Your code should look similar to the following now.
<iframe id="ifFacebook" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fexample.com%2Fpage%2Fto%2Flike&layout=standard&show_faces=true&width=450&action=like&colorscheme=light&height=80"
scrolling="no" frameborder="0"
style="border:none; overflow:hidden; width:450px; height:80px;"
allowTransparency="true"></iframe>
Lay Down Some jQuery
jQuery is the most convenient and easiest way to dynamically change the IFRAME element to work, by adding the current URL that the element is on. What makes it even more awesome, is that it comes prepackaged with DNN!
We need a snippet of jQuery that automatically injects the URL of the current page into the Facebook IFRAME we have. Something like this will do:
var currentURL = encodeURIComponent(location.href);
jQuery(document).ready(function(){
jQuery('#ifFacebook').attr('src', jQuery('#ifFacebook').attr('src') + currentURL);
});
The above snippet first URL-encodes the current URL, and then appends it to the end of the existing value in the SRC attribute of the IFRAME.
Putting It All Together
Now that we have the two pieces of the code that we need, this is what it looks like when you put it together:
<iframe id="ifFacebook" src="http://www.facebook.com/plugins/like.php?show_faces=true&width=450&action=like&colorscheme=light&height=80&href=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>
<script language="javascript" type="text/javascript">
var currentURL = encodeURIComponent(location.href);
jQuery(document).ready(function(){ jQuery('#ifFacebook').attr('src', jQuery('#ifFacebook').attr('src') + currentURL); });
</script>
Your code might look slightly different, depending on the Like Button options you chose, and how you implemented the ID and/or CLASS attributes in the IFRAME tag.
Add the Code to Your Page
The final act to this tutorial is to add the code to your site. There are numerous ways to do this, some good and some bad. Probably the best way to do this is to use a module like PageBlaster to inject the code to your site, at will, anywhere that you’d like. It’s very flexible.
What I did was closer to being the wrong thing to do though. I actually put the code into the ViewEntry.ascx user control of the Blog Module. This isn’t a big deal to me, because I always have a forked version of the Blog Module. If you are not comfortable altering and maintaining a fork of the Blog Module, do this another way...
I injected my code snippet into the aforementioned file, just above the chicklets. If you like the placement, look for the “ShareBadgePRO_Toolbar” DIV, and put the code snippet above it.
I also made it a bit prettier in my site, by adding a small bit of CSS to remove some of the padding from the footer of the blog entry.
.BlogFooter { padding-bottom: 20px; }
That’s it! As you can see below, I have a Like Button at the bottom of each of my blog entries. If you LIKE this post, make sure you click the Like Button at the bottom of this post! :)
6 comment(s) so far...
Re: Adding a Facebook Like Button to Your DotNetNuke Blog
I think I just identified a pretty major bug in Facebook's 'Like' button, which allows webmasters to write data on FB's social graph, which cannot be 'deleted' *if* they should they make a mistake when they initially implement the code into their website.
And due to the way FB presents the tool and the code generator to the public, many webmasters do not read the docs (and just add the cut and paste code) and fail to add the meta tags required to admin the tool... which causes permanent problems later, if they ever want to admin the tool. In fact, most webmasters with this issue dont even know there an admin panel should have been dynamically generated for them when they first added the code, which they forever locked themselves out of.
They post will probably be deleted soon... because 'stickied' threads in the developers' forums tend to do that... but here is the most current status of the issue:
forum.developers.facebook.net/viewtopic.php?pid=263145 ...this is a pretty major bug, imo. They need to stop the bleeding by getting a note on that 'Like' Button Code Generator asap, then trying to figure out a way they can remap the admins to the domains.
But now they have an issue with authenticating the admin really has the website's permission to admin their tool... and that's a little sticky. I suggested they email the 'tech contact' listed in the whois of the domain in question. Otherwise you have a hijacking risk.
Another issue is simply that the easiest solution may involve deleting the data associated with the fan's actions, which means people waiting for the solution will eventually have their count go back to zero. And webmasters don't get enough loves as is so I doubt they will take losing all their hard won contacts without a bit of a backlash. Especially when many are bloggers. I guess we'll see what they say Monday.
Anyway, thought you might be interested in following this.
Take care, Rob
By rob on
9/1/2010
|
Re: Adding a Facebook Like Button to Your DotNetNuke Blog
Whoa... Nice info, Rob. Thanks. I missed it myself, so I will be looking into it.
By Will on
9/1/2010
|
Re: Adding a Facebook Like Button to Your DotNetNuke Blog
Has anyone tried integrating Facebook Comment API into the DNN blog?
I was also curious why people use Technorati Tags over the standard DNN blog tags?
Thanks,
Sean
By Sean on
9/3/2010
|
Re: Adding a Facebook Like Button to Your DotNetNuke Blog
I haven't tried implementing any custom comments API's myself, but being pretty intimate with the blog code, I know it would be pretty easy from a technical perspective.
I still use the technorati tags because I already have been using them for a long time, and I plan to have a campaign where I take advantage of them through technorati. You can get a lot of exposure there. I can't speak for anyone else though.
By Will on
9/3/2010
|
Re: Adding a Facebook Like Button to Your DotNetNuke Blog
currently I am trying to integrate facebook comments into my dnn site. but i cant figure out a way to change the value of href of newly introduced fb:comments tag on the fly. any advice??
By qamar uddin on
4/16/2011
|
Re: Adding a Facebook Like Button to Your DotNetNuke Blog
@Qamar: Try taking a look at the Facebook Widget in the Widget Suite.
By Will on
4/21/2011
|