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:
- Log into Facebook and then go and add the Facebook Developers Application
- Click the big button at the top right to Set Up a New Application
- Enter a name and agree to the terms (you read them all, right?)
- 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.
- 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.
- 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.
- Go to the list of your Facebook Apps and click on the app we just created on the left
- Click “Create Feed Template” in the list of links on the right
- Make sure your correct App is selected in the box, then click Next
- In the “One Line Template” box, paste this exact text
<span style="color: #606060;"> 1:</span> {*actor*} commented on the {*blog*} post {*post*}.- 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;">"<a href='http://test.domain.com'>My Blog Name</a>"</span>, <span style="color: #006080;">"post"</span>:<span style="color: #006080;">"<a href='http://test.domain.com/post-url/'>Test Post Title</a>"</span>}- Click Update Preview and make sure that you’re happy with the News Feed format (if not, change the One Line Template string)
- Click Next
- Now click Skip (and ignore/Okay any errors) until you get to the final page and then click “Register Template Bundle”
- 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> <!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>><span style="color: #606060;"> 2:</span> <html xmlns=<span style="color: #006080;">"http://www.w3.org/1999/xhtml"</span>><span style="color: #606060;"> 3:</span> <head><span style="color: #606060;"> 4:</span> <title>Cross-Domain Receiver Page</title><span style="color: #606060;"> 5:</span> </head><span style="color: #606060;"> 6:</span> <body><span style="color: #606060;"> 7:</span> <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>></script><span style="color: #606060;"> 8:</span> </body><span style="color: #606060;"> 9:</span> </html>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> <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> <?php language_attributes(); ?>>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> <?php wp_enqueue_script(<span style="color: #006080;">'jquery'</span>); ?>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> <div id=<span style="color: #006080;">"comment-user-details"</span>><span style="color: #606060;"> 2:</span> <fb:login-button length=<span style="color: #006080;">"long"</span> onlogin=<span style="color: #006080;">"update_user_details();"</span>></fb:login-button><span style="color: #606060;"> 3:</span><span style="color: #606060;"> 4:</span> <p style=<span style="color: #006080;">"clear:left;"</span>><strong>Or enter your details below:</strong></p><span style="color: #606060;"> 5:</span><span style="color: #606060;"> 6:</span> <p><label <span style="color: #0000ff;">for</span>=<span style="color: #006080;">"name"</span>>Name <?php <span style="color: #0000ff;">if</span> ($req) echo <span style="color: #006080;">"(required)"</span>; ?></label><br /><span style="color: #606060;"> 7:</span> <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;">"<?php echo $comment_author; ?>"</span> size=<span style="color: #006080;">"50"</span> tabindex=<span style="color: #006080;">"1"</span> /></p><span style="color: #606060;"> 8:</span><span style="color: #606060;"> 9:</span> <p><label <span style="color: #0000ff;">for</span>=<span style="color: #006080;">"email"</span>>Email Address <?php <span style="color: #0000ff;">if</span> ($req) echo <span style="color: #006080;">"(required)"</span>; ?></label><br /><span style="color: #606060;"> 10:</span> <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;">"<?php echo $comment_author_email; ?>"</span> size=<span style="color: #006080;">"50"</span> tabindex=<span style="color: #006080;">"2"</span> /></p><span style="color: #606060;"> 11:</span><span style="color: #606060;"> 12:</span> <p><label <span style="color: #0000ff;">for</span>=<span style="color: #006080;">"url"</span>>Website</label><br /><span style="color: #606060;"> 13:</span> <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;">"<?php echo $comment_author_url; ?>"</span> size=<span style="color: #006080;">"50"</span> tabindex=<span style="color: #006080;">"3"</span> /></p><span style="color: #606060;"> 14:</span> </div>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> <form action=<span style="color: #006080;">"<?php echo get_option('siteurl'); ?>/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>>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> <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>></script><span style="color: #606060;"> 2:</span> <style type=<span style="color: #006080;">"text/css"</span>><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> </style><span style="color: #606060;"> 8:</span><span style="color: #606060;"> 9:</span> <script type=<span style="color: #006080;">"text/javascript"</span>><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;">"<span id='fb-user'>"</span> +<span style="color: #606060;"> 16:</span> <span style="color: #006080;">"<fb:profile-pic uid='loggedinuser' facebook-logo='true'></fb:profile-pic>"</span> +<span style="color: #606060;"> 17:</span> <span style="color: #006080;">"<span id='fb-msg'><strong>Hi <fb:name uid='loggedinuser' useyou='false'></fb:name>!</strong><br />You are logged in with your Facebook account. "</span> +<span style="color: #606060;"> 18:</span> <span style="color: #006080;">"<a href='#' onclick='FB.Connect.logoutAndRedirect("<?php the_permalink() ?>"); return false;'>Logout</a>"</span> +<span style="color: #606060;"> 19:</span> <span style="color: #006080;">"</span></span>"</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>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;">'<a href="<?php bloginfo('</span>home<span style="color: #006080;">') ?>"><?php addslashes(bloginfo('</span>name<span style="color: #006080;">')) ?></a>'</span>, <span style="color: #006080;">'post'</span>:<span style="color: #006080;">'<a href="<?php the_permalink() ?>"><?php addslashes(the_title()) ?></a>'</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> </script>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
Pierre Stoffe says
Test
Cotton Rohrscheib says
Testing to see if this shows up in Facebook now.
Benny Betatester says
En Ie6 y Ie7 te da error abajo cuando te logueas en facebook
Cotton Rohrscheib says
Hola Benny –
¿Yo no soy seguramente cuál error que usted se refiere a, puede ser usted más específico?
Gracias,
Cotton
Rizwan Akram says
nice
WordPress Movie Plugin says
That’s definitely a great topic to write a post about, and I do plan on expanding upon various aspects of this post.