Cotton Rohrscheib

The Cotton Club Blog & Podcast

  • Home
  • Bio
    • Resume
  • Blog
    • Faith & Family
    • Marketing & Tech
    • Farm & Business
    • Entertainment
    • Health & Wellness
    • Urban Farming
    • Weekend Projects
  • Media
    • Newsletter
    • Photo Galleries
    • Instagram Feed
    • Video Archives
    • Podcasts
    • Music Playlists
  • Books
  • Connect
    • Rohrscheib Capital
    • Disclaimer
    • Privacy Policy
You are here: Home / Marketing & Tech / Beau Lebens Facebook Connect

Beau Lebens Facebook Connect

January 5, 2009 by Cotton Rohrscheib 6 Comments

I have going back and forth w/ Beau Lebens today after reading his How To blog post on Dented Reality discussing his variation of the Facebook Connect application and after testing it today, it’s a very clean plug-in and it seems to work very smoothly.

In his blog post he also outlines all of the requirements to get everything working correctly and provides some source code examples as well.  He basically rolled his own solution and has it running on Resume Donkey, check out his blog post below:

2008-12-23: There were a number of problems with the code samples in this post previously due to some WordPress formatting problems. They are all corrected now, and you should be able to follow through this post and get this working on your own blog quite easily.

2008-12-26: Fixed a bug that caused the JS to overwrite details on a non-FB Connect comment as well. Also changed the fake email address that’s stored to include the user’s FB user ID.

In case you’ve been living under a no-technology-news rock for the last few weeks, you’ll know that Facebook Connect was released recently. I had been seeing/hearing a lot about it, including this video at Mashable, showing how to implement FB Connect in 8 minutes. So when my friend Morgan from BlownMortgage asked me if I’d be able to help him implement it on his new resume-editing site ResumeDonkey.com, I figured “how hard could it be” and said yes. Although it definitely didn’t take 8 minutes, I got it done, so I thought I’d post some details on the specific approach I used for ResumeDonkey.com.

Before I rolled my own solution, I took a good look at a few of the existing WordPress options including:

  • The WP-FBConnect plugin, made by Facebook,
  • Another WordPress plugin made by Sociable, and
  • The Community Facebook Connect Plugin, by Jesse Stay

None of these worked quite how Morgan and I had discussed, so I decided to make my own, lightweight solution. Before editing any actual theme files, there’s some prep-work to be done, so:

  1. Log into Facebook and then go and add the Facebook Developers Application
  2. Click the big button at the top right to Set Up a New Application
  3. Enter a name and agree to the terms (you read them all, right?)
  4. On the next page, enter the base URL of your website in the “Callback URL” field. MAKE SURE you use the correct preference for your website as far as www. or no www. is concerned, and preferably enforce that on your website using a plugin or something. If you enter http://www.domain.com here, and someone accesses your site as http://domain.com, then your FB Connect integration will break and throw a warning about being on the wrong URL.
  5. You can also set some sexy icons/logos to appear in the News Feed of people who comment on your blog, but I’ll let you handle that.
  6. Get a copy of the “API Key” at the top of this page, you’ll need that later.

OK, now we need to register a “template bundle”, which will be used to post updates to the News Feed of people who comment on your blog.

  1. Go to the list of your Facebook Apps and click on the app we just created on the left
  2. Click “Create Feed Template” in the list of links on the right
  3. Make sure your correct App is selected in the box, then click Next
  4. In the “One Line Template” box, paste this exact text
    <span style="color: #606060;">   1:</span> {*actor*} commented on the {*blog*} post {*post*}.
  5. In the “Sample Template Data” box, paste this (make sure quote marks are still  quotes and not fancy curly-quotes)
    <span style="color: #606060;">   1:</span> {<span style="color: #006080;">"blog"</span>:<span style="color: #006080;">"&lt;a href='http://test.domain.com'&gt;My Blog Name&lt;/a&gt;"</span>, <span style="color: #006080;">"post"</span>:<span style="color: #006080;">"&lt;a href='http://test.domain.com/post-url/'&gt;Test Post Title&lt;/a&gt;"</span>}
  6. Click Update Preview and make sure that you’re happy with the News Feed format (if not, change the One Line Template string)
  7. Click Next
  8. Now click Skip (and ignore/Okay any errors) until you get to the final page and then click “Register Template Bundle”
  9. It will give you a Template Bundle ID, and you’ll want to get a copy of that, because we’ll need it later as well.

OK. Now you’ve got a registered and configured (roughly) App on Facebook, time to get dirty on your own blog. Create a file in the root of your domain and call it “xd_receiver.htm”, then copy the following code into it:

<span style="color: #606060;">   1:</span> &lt;!DOCTYPE html PUBLIC <span style="color: #006080;">"-//W3C//DTD XHTML 1.0 Strict//EN"</span> <span style="color: #006080;">"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"</span>&gt;
<span style="color: #606060;">   2:</span> &lt;html xmlns=<span style="color: #006080;">"http://www.w3.org/1999/xhtml"</span>&gt;
<span style="color: #606060;">   3:</span> &lt;head&gt;
<span style="color: #606060;">   4:</span> &lt;title&gt;Cross-Domain Receiver Page&lt;/title&gt;
<span style="color: #606060;">   5:</span> &lt;/head&gt;
<span style="color: #606060;">   6:</span> &lt;body&gt;
<span style="color: #606060;">   7:</span> &lt;script src=<span style="color: #006080;">"http://static.ak.facebook.com/js/api_lib/v0.4/XdCommReceiver.js"</span> type=<span style="color: #006080;">"text/javascript"</span>&gt;&lt;/script&gt;
<span style="color: #606060;">   8:</span> &lt;/body&gt;
<span style="color: #606060;">   9:</span> &lt;/html&gt;

Add the “fb” XML namespace to the header.php file in your theme. Mine ended up looking like this (in PHP):

<span style="color: #606060;">   1:</span> &lt;html xmlns=<span style="color: #006080;">"http://www.w3.org/1999/xhtml"</span> xmlns:fb=<span style="color: #006080;">"http://www.facebook.com/2008/fbml"</span> &lt;?php language_attributes(); ?&gt;&gt;

And also drop in a reference to jQuery if you don’t already use it in your theme. It’s bundled with WordPress so you can reference it like this (anywhere before the call to “wp_head()” in your header.php):

<span style="color: #606060;">   1:</span> &lt;?php wp_enqueue_script(<span style="color: #006080;">'jquery'</span>); ?&gt;

Then you’ll want to edit comments.php (assuming you’re using a relatively normal theme), and make some changes to add the FB Connect button. Find the part where a user would normally enter their name/email/URL and change it to look something like this:

<span style="color: #606060;">   1:</span> &lt;div id=<span style="color: #006080;">"comment-user-details"</span>&gt;
<span style="color: #606060;">   2:</span> &lt;fb:login-button length=<span style="color: #006080;">"long"</span> onlogin=<span style="color: #006080;">"update_user_details();"</span>&gt;&lt;/fb:login-button&gt;
<span style="color: #606060;">   3:</span>
<span style="color: #606060;">   4:</span> &lt;p style=<span style="color: #006080;">"clear:left;"</span>&gt;&lt;strong&gt;Or enter your details below:&lt;/strong&gt;&lt;/p&gt;
<span style="color: #606060;">   5:</span>
<span style="color: #606060;">   6:</span> &lt;p&gt;&lt;label <span style="color: #0000ff;">for</span>=<span style="color: #006080;">"name"</span>&gt;Name &lt;?php <span style="color: #0000ff;">if</span> ($req) echo <span style="color: #006080;">"(required)"</span>; ?&gt;&lt;/label&gt;&lt;br /&gt;
<span style="color: #606060;">   7:</span> &lt;input type=<span style="color: #006080;">"text"</span> name=<span style="color: #006080;">"author"</span> id=<span style="color: #006080;">"name"</span> <span style="color: #0000ff;">value</span>=<span style="color: #006080;">"&lt;?php echo $comment_author; ?&gt;"</span> size=<span style="color: #006080;">"50"</span> tabindex=<span style="color: #006080;">"1"</span> /&gt;&lt;/p&gt;
<span style="color: #606060;">   8:</span>
<span style="color: #606060;">   9:</span> &lt;p&gt;&lt;label <span style="color: #0000ff;">for</span>=<span style="color: #006080;">"email"</span>&gt;Email Address &lt;?php <span style="color: #0000ff;">if</span> ($req) echo <span style="color: #006080;">"(required)"</span>; ?&gt;&lt;/label&gt;&lt;br /&gt;
<span style="color: #606060;">  10:</span> &lt;input type=<span style="color: #006080;">"text"</span> name=<span style="color: #006080;">"email"</span> id=<span style="color: #006080;">"email"</span> <span style="color: #0000ff;">value</span>=<span style="color: #006080;">"&lt;?php echo $comment_author_email; ?&gt;"</span> size=<span style="color: #006080;">"50"</span> tabindex=<span style="color: #006080;">"2"</span> /&gt;&lt;/p&gt;
<span style="color: #606060;">  11:</span>
<span style="color: #606060;">  12:</span> &lt;p&gt;&lt;label <span style="color: #0000ff;">for</span>=<span style="color: #006080;">"url"</span>&gt;Website&lt;/label&gt;&lt;br /&gt;
<span style="color: #606060;">  13:</span> &lt;input type=<span style="color: #006080;">"text"</span> name=<span style="color: #006080;">"url"</span> id=<span style="color: #006080;">"url"</span> <span style="color: #0000ff;">value</span>=<span style="color: #006080;">"&lt;?php echo $comment_author_url; ?&gt;"</span> size=<span style="color: #006080;">"50"</span> tabindex=<span style="color: #006080;">"3"</span> /&gt;&lt;/p&gt;
<span style="color: #606060;">  14:</span> &lt;/div&gt;

Just above this block, you should also find the start of the <form> tag for posting a comment, you want to add the “onsubmit” attribute to it so that it looks something like this:

<span style="color: #606060;">   1:</span> &lt;form action=<span style="color: #006080;">"&lt;?php echo get_option('siteurl'); ?&gt;/wp-comments-post.php"</span> method=<span style="color: #006080;">"post"</span> id=<span style="color: #006080;">"commentform"</span> onsubmit=<span style="color: #006080;">"update_form_values();"</span>&gt;

The important parts there are that it’s all wrapped in a DIV or SPAN with id=”comment-user-details” and then obviously the <fb:login-button> stuff. Now further down (I went right down to the bottom of the comments.php file actually), add this code:

<span style="color: #606060;">   1:</span> &lt;script src=<span style="color: #006080;">"http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php"</span> type=<span style="color: #006080;">"text/javascript"</span>&gt;&lt;/script&gt;
<span style="color: #606060;">   2:</span> &lt;style type=<span style="color: #006080;">"text/css"</span>&gt;
<span style="color: #606060;">   3:</span> #fb-user { border: 1px dotted #C0C0C0; padding: 5px; display: block; height: 48px; }
<span style="color: #606060;">   4:</span> #fb-msg { <span style="color: #0000ff;">float</span>:left; }
<span style="color: #606060;">   5:</span> .fb_profile_pic_rendered { margin-right: 5px; }
<span style="color: #606060;">   6:</span> a.FB_Link img { <span style="color: #0000ff;">float</span>: left; }
<span style="color: #606060;">   7:</span> &lt;/style&gt;
<span style="color: #606060;">   8:</span>
<span style="color: #606060;">   9:</span> &lt;script type=<span style="color: #006080;">"text/javascript"</span>&gt;
<span style="color: #606060;">  10:</span> var fb_connect_user = <span style="color: #0000ff;">false</span>;
<span style="color: #606060;">  11:</span> function update_user_details() {
<span style="color: #606060;">  12:</span> fb_connect_user = <span style="color: #0000ff;">true</span>;
<span style="color: #606060;">  13:</span> <span style="color: #008000;">// Show their FB details</span>
<span style="color: #606060;">  14:</span> <span style="color: #0000ff;">if</span> (!jQuery(<span style="color: #006080;">'#fb-user'</span>).length) {
<span style="color: #606060;">  15:</span> jQuery(<span style="color: #006080;">'#comment-user-details'</span>).hide().after(<span style="color: #006080;">"&lt;span id='fb-user'&gt;"</span> +
<span style="color: #606060;">  16:</span> <span style="color: #006080;">"&lt;fb:profile-pic uid='loggedinuser' facebook-logo='true'&gt;&lt;/fb:profile-pic&gt;"</span> +
<span style="color: #606060;">  17:</span> <span style="color: #006080;">"&lt;span id='fb-msg'&gt;&lt;strong&gt;Hi &lt;fb:name uid='loggedinuser' useyou='false'&gt;&lt;/fb:name&gt;!&lt;/strong&gt;&lt;br /&gt;You are logged in with your Facebook account. "</span> +
<span style="color: #606060;">  18:</span> <span style="color: #006080;">"&lt;a href='#' onclick='FB.Connect.logoutAndRedirect("&lt;?php the_permalink() ?&gt;"); return false;'&gt;Logout&lt;/a&gt;"</span> +
<span style="color: #606060;">  19:</span> <span style="color: #006080;">"&lt;/span&gt;&lt;/span&gt;"</span>);
<span style="color: #606060;">  20:</span> }
<span style="color: #606060;">  21:</span>
<span style="color: #606060;">  22:</span> <span style="color: #008000;">// Refresh the DOM</span>
<span style="color: #606060;">  23:</span> FB.XFBML.Host.parseDomTree();
<span style="color: #606060;">  24:</span> }
<span style="color: #606060;">  25:</span>
<span style="color: #606060;">  26:</span> function update_form_values() {
<span style="color: #606060;">  27:</span> <span style="color: #0000ff;">if</span> (fb_connect_user) {
<span style="color: #606060;">  28:</span> profile = jQuery(<span style="color: #006080;">'#fb-user'</span>).find(<span style="color: #006080;">'.FB_ElementReady .FB_Link'</span>)[1][<span style="color: #006080;">'href'</span>];
<span style="color: #606060;">  29:</span> user_id = profile.substring(profile.indexOf(<span style="color: #006080;">'?id='</span>)+4);
<span style="color: #606060;">  30:</span> jQuery(<span style="color: #006080;">'#url'</span>).val(profile); <span style="color: #008000;">// FB profile URL</span>
<span style="color: #606060;">  31:</span> jQuery(<span style="color: #006080;">'#email'</span>).val(user_id+<span style="color: #006080;">'@facebook.com'</span>); <span style="color: #008000;">// Can't get a real one from FB unfortunately. This saves their user id @facebook.com</span>
<span style="color: #606060;">  32:</span> jQuery(<span style="color: #006080;">'#fb-user'</span>).find(<span style="color: #006080;">'.FB_ElementReady .FB_Link'</span>).each(function(i){ <span style="color: #0000ff;">if</span> (i==1) { jQuery(<span style="color: #006080;">'#name'</span>).val(jQuery(<span style="color: #0000ff;">this</span>).text()); } }); <span style="color: #008000;">// Gets their name from the DOM</span>
<span style="color: #606060;">  33:</span> setCookie(<span style="color: #006080;">'fb_connect'</span>, <span style="color: #006080;">'yes'</span>);
<span style="color: #606060;">  34:</span> }
<span style="color: #606060;">  35:</span> }
<span style="color: #606060;">  36:</span>
<span style="color: #606060;">  37:</span> function setCookie(c_name,<span style="color: #0000ff;">value</span>,expiredays) {
<span style="color: #606060;">  38:</span> var exdate=<span style="color: #0000ff;">new</span> Date();
<span style="color: #606060;">  39:</span> exdate.setDate(exdate.getDate()+expiredays);
<span style="color: #606060;">  40:</span> document.cookie=c_name+ <span style="color: #006080;">"="</span> +escape(<span style="color: #0000ff;">value</span>)+((expiredays==<span style="color: #0000ff;">null</span>) ? <span style="color: #006080;">""</span> : <span style="color: #006080;">";expires="</span>+exdate.toGMTString());
<span style="color: #606060;">  41:</span> }
<span style="color: #606060;">  42:</span>
<span style="color: #606060;">  43:</span> function getCookie(c_name) {
<span style="color: #606060;">  44:</span> <span style="color: #0000ff;">if</span> (document.cookie.length&gt;0) {
<span style="color: #606060;">  45:</span> c_start=document.cookie.indexOf(c_name + <span style="color: #006080;">"="</span>);
<span style="color: #606060;">  46:</span> <span style="color: #0000ff;">if</span> (c_start!=-1) {
<span style="color: #606060;">  47:</span> c_start=c_start + c_name.length+1;
<span style="color: #606060;">  48:</span> c_end=document.cookie.indexOf(<span style="color: #006080;">";"</span>,c_start);
<span style="color: #606060;">  49:</span> <span style="color: #0000ff;">if</span> (c_end==-1) c_end=document.cookie.length;
<span style="color: #606060;">  50:</span> <span style="color: #0000ff;">return</span> unescape(document.cookie.substring(c_start,c_end));
<span style="color: #606060;">  51:</span> }
<span style="color: #606060;">  52:</span> }
<span style="color: #606060;">  53:</span> <span style="color: #0000ff;">return</span> <span style="color: #006080;">""</span>;
<span style="color: #606060;">  54:</span> }
<span style="color: #606060;">  55:</span>
<span style="color: #606060;">  56:</span> FB.init(<span style="color: #006080;">"YOUR-FACEBOOK-API-KEY"</span>, <span style="color: #006080;">"/xd_receiver.htm"</span>);
<span style="color: #606060;">  57:</span> FB.Connect.ifUserConnected(update_user_details);
<span style="color: #606060;">  58:</span> <span style="color: #0000ff;">if</span> (getCookie(<span style="color: #006080;">'fb_connect'</span>) == <span style="color: #006080;">'yes'</span>) {
<span style="color: #606060;">  59:</span> setCookie(<span style="color: #006080;">'fb_connect'</span>, <span style="color: #0000ff;">null</span>);
<span style="color: #606060;">  60:</span> FB.Connect.showFeedDialog(YOUR-TEMPLATE-BUNDLE-ID, {<span style="color: #006080;">'blog'</span>:<span style="color: #006080;">'&lt;a href="&lt;?php bloginfo('</span>home<span style="color: #006080;">') ?&gt;"&gt;&lt;?php addslashes(bloginfo('</span>name<span style="color: #006080;">')) ?&gt;&lt;/a&gt;'</span>, <span style="color: #006080;">'post'</span>:<span style="color: #006080;">'&lt;a href="&lt;?php the_permalink() ?&gt;"&gt;&lt;?php addslashes(the_title()) ?&gt;&lt;/a&gt;'</span>}, <span style="color: #0000ff;">null</span>, <span style="color: #0000ff;">null</span>, <span style="color: #0000ff;">null</span>, FB.RequireConnect.promptConnect);
<span style="color: #606060;">  61:</span> }
<span style="color: #606060;">  62:</span> &lt;/script&gt;

For those of you paying any attention to what you’re copy-pasting, you would have noticed that there are 2 important things you need to replace in that last block of code. Go back now and replace “YOUR-FACEBOOK-API-KEY” and “YOUR-TEMPLATE-BUNDLE-ID” with the appropriate values from the beginning of this process. YOUR-FACEBOOK-API-KEY should be replaced with the 32-character string from the Facebook App config, and should include double-quotes around it in the code above. The YOUR-TEMPLATE-BUNDLE-ID should not have quotes around it.

Save everything and upload it (if you were working offline). If all has gone well, you should now get a FB Connect button on your comments (you need to log out of WordPress to see it), and when you click it, you should connect to FB, then be able to post a comment.

When a Facebook user comments on your blog now, their name will be loaded from Facebook, their profile URL will be used as their URL, and the email address will be recorded as “[email protected]” (their API doesn’t allow you to actually get it, to avoid spam I assume).

Enjoy.

HOWTO: Implement Facebook Connect on WordPress (in reality) « Dented Reality

Share this post on:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pinterest (Opens in new window) Pinterest

Related

About Cotton Rohrscheib

The Cotton Club is a monthly podcast hosted by me, Cotton Rohrscheib. I'm a 52 year old entrepreneur w/ ADHD, OCD (and now AARP) that refuses to grow up as I grow old. I have collaborated and invested in hundreds of projects throughout my career in multiple industries such as; technology, healthcare, and agriculture. I also have 25 years experience in the marketing industry as a co-founder of an award-winning advertising agency. I will undoubtedly cover a wide variety of topics on my podcast while sharing some really crazy stories and situations that I've been fortunate to witness firsthand. I also have a book coming out in 2025 titled, "Mistakes were Made"

Comments

  1. Pierre Stoffe says

    January 2, 2009 at 6:06 pm

    Test

    Reply
  2. Cotton Rohrscheib says

    January 2, 2009 at 6:55 pm

    Testing to see if this shows up in Facebook now.

    Reply
  3. Benny Betatester says

    January 5, 2009 at 5:50 am

    En Ie6 y Ie7 te da error abajo cuando te logueas en facebook

    Reply
  4. Cotton Rohrscheib says

    January 5, 2009 at 5:21 pm

    Hola Benny –
    ¿Yo no soy seguramente cuál error que usted se refiere a, puede ser usted más específico?
    Gracias,
    Cotton

    Reply
  5. Rizwan Akram says

    February 3, 2009 at 4:34 am

    nice

    Reply
  6. WordPress Movie Plugin says

    October 7, 2010 at 12:01 am

    That’s definitely a great topic to write a post about, and I do plan on expanding upon various aspects of this post.

    Reply

Please Drop Your Questions or CommentsCancel reply

Let’s Connect

  • Email
  • Facebook
  • Instagram
  • LinkedIn
  • Twitter

Recent Updates

  • EP:032 – Cotton Rohrscheib & Diana DeHart
  • Challenges & Opportunities Going into 2025
  • Find us at the 2025 Arkansas Women in Agriculture Conference in Hot Springs, Arkansas
  • Be Sure to Checkout FBN’s Farmers First™  Crop Nutrition & Adjuvant Lineup for 2025
  • What we all need in Dark Times…

Blog Categories

  • Blog (419)
  • Entertainment (376)
  • Faith & Family (147)
  • Farm & Business (288)
  • Health & Wellness (33)
  • Marketing & Tech (584)
  • Podcasts (31)
  • Urban Farming (20)
  • Weekend Projects (1)

Listen & Subscribe

Blog Archives

Join the Cotton Club!

 

Content Copyright: 2001-2025
Cotton Rohrscheib | Rohrscheib Capital