DNN Blog

Aug 14

Posted by: Will Strohl
8/14/2010 10:01 PM  RssIcon

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.

Facebook Like Button

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.

Blog Module: Facebook Button Position

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! :)

Example Facebook Like Button

This post is cross-posted from my personal blog site.

Technorati Tags: ,,,,,,,,,

Tags:
Categories: Development
Location: Blogs Parent Separator Will Strohl

10 comment(s) so far...


Gravatar

Re: Adding a Facebook Like Button to Your DotNetNuke Blog

All the pictures are missing.

By Andre van den Berg on   8/15/2010 11:05 AM
Gravatar

Re: Adding a Facebook Like Button to Your DotNetNuke Blog

I posted this last night first, forgetting to update the image paths. I did, however, immediately repost with the image paths updated. Unfortunately, it looks like the second post was made in the middle of some site maintenance - wiping out my update.

Thanks for letting me know. :)

By Will Strohl on   8/15/2010 11:08 AM
Gravatar

Re: Adding a Facebook Like Button to Your DotNetNuke Blog

Your script requires to have jQuery installed on every visitor's machine. I find that highly improbable. This should work on any machine:

var currentURL = encodeURIComponent(location.href);
var i = eval("document.all('ifFacebook')");
i.src = i.src + currentURL;



By Tom Wellige on   8/16/2010 7:15 AM
Gravatar

Re: Adding a Facebook Like Button to Your DotNetNuke Blog

@Tom: Actually, that is incorrect. This solution requires that the WEBSITE have jQuery available to it. The website could care less what the client machine has installed (other than a first class web browser) - and unless something has changed, you cannot "install" jQuery on a visitor's machine. jQuery is installed on all DNN sites in the version 5 series by default (except for upgraded sites, where it needs to be enabled).

By Will Strohl on   8/16/2010 7:18 AM
Gravatar

Re: Adding a Facebook Like Button to Your DotNetNuke Blog

@Tom: Jquery is a javascript framework, not a browser plugin. Your browser downloads the jquery javascript on browser page render just like any javascript file. Then the jquery runs client side just like javascript would because after all, it is just javascript. Every browser will automatically download the jquery .js file and will execute regardless.

By Kelly Edvalson on   8/22/2010 7:35 PM
Gravatar

Re: Adding a Facebook Like Button to Your DotNetNuke Blog

Would it be possible to do this so that the Like button is for the entire web page instead of just a blog entry? I ask this because I need to be able to put a "Like" button on every page. I am using DNN 4.9.5 and need to know if this is possible. If it is, could we get a good walk-through of that or, if there is a module or an implementation of this available, please let me know.

Thanks guys!

By Mark Gordon on   9/3/2010 6:39 AM
Gravatar

Re: Adding a Facebook Like Button to Your DotNetNuke Blog

@Mark: Absolutely. The technique is the same. You might also consider watching the latest DNN Hero video for that kind of implementation too.

By Will Strohl on   9/3/2010 6:41 AM
Gravatar

Re: Adding a Facebook Like Button to Your DotNetNuke Blog

Hi,

I've tried to get the like button working, but it only works half. The button does show in every blogpost (I want people to like individual posts), but for some reason when I click the like button, it gives an error. When I try to debugg with the FB debugger, it states that it's missing those Open Graph proper.

Are those Open Graph properties required in my viewblog.ascx file?

By Jan Peter on   12/28/2011 8:23 AM
Gravatar

Re: Adding a Facebook Like Button to Your DotNetNuke Blog

@Jan: The open graph protocol wasn't originally required when this blog post was written. You can use the Page Header field in your to add those fields, or use another tool like a custom module or skin widget to add those fields.

By Will Strohl on   12/28/2011 9:12 AM
Gravatar

Re: Adding a Facebook Like Button to Your DotNetNuke Blog

@will: Thank you for your reply. I fiddled a bit more with the code you provide in this post and I got the button to work.
I copied and pasted the iframe en jquery in my viewblog.ascx file but I didn't bother to take a closer look at the code (I completely and blindly trusted the code you provided). Turned out that my site didn't liked the way the Iframe was closed. So I fixed that bit and the button works like a charm.

Best wishes for 2012 and thanks again for the reply!

By Jan Peter on   12/30/2011 8:22 AM
Attend A Webinar
Free Demo Site
Download DotNetNuke Professional Edition Trial
Have Someone Contact Me
Have Someone Contact Me
DotNetNuke Store

Like Us on Facebook Join our Network on LinkedIn Follow DNN Corporate on Twitter Follow DNN on Twitter

Advertisers

Telerik JustCode Free
Exact Target Exec Alert
PowerDNN

Sponsors

DotNetNuke Corporation

DotNetNuke Corp. is the steward of the DotNetNuke open source project, the most widely adopted Web Content Management Platform for building web sites and web applications on Microsoft. Organizations use DotNetNuke to quickly develop and deploy interactive and dynamic web sites, intranets, extranets and web applications. The DotNetNuke platform is available in a free Community and subscription-based Professional and Enterprise Editions with an Elite Support option. DotNetNuke Corp. also operates the DotNetNuke Store where users purchase third party apps for the platform.