How to Make Facebook Connect Work With WP Super Cache

-Facebook Connect Button Screenshot-When I started to implement Facebook Connect last week I thought it was going to be quick and easy, boy was I wrong. While Facebook Connect was working as it should, the problem that I ran into was a problem that many people have run into when developing plugins for WordPress. If you have installed WP Cache, or in my case WP Super Cache, it’s pretty much guaranteed that you’ll have issues with displaying a user’s information after they login.

After a week of trying to develop an AJAX workaround and a million other models, with the help of Adam Hupp at Facebook this morning I was able to narrow down the possible errors. Apparently WP Super Cache has a filtering system which parses specific user cookies and won’t cache pages for those individuals that have those cookies (usually a small percentage of your site’s visitors).

There are two places that WP Super Cache looks at for filtering user cookies. The first (which I assume is the case for most users) is in the .htaccess file in your document root. If this is how your site is configured, you’ll need to find the following code in your .htaccess file:

# BEGIN WPSuperCache
RewriteCond %{REQUEST_URI} !^.*[^/]$
RewriteCond %{REQUEST_URI} !^.*//.*$
RewriteCond %{QUERY_STRING} !.*=.*
RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress|wp-postpass_).*$
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{DOCUMENT_ROOT}/wordpress/wp-content/cache/supercache/%{HTTP_HOST}/$1/index.html.gz -f
RewriteRule ^(.*) /wordpress/wp-content/cache/supercache/%{HTTP_HOST}/$1/index.html.gz [L]

RewriteCond %{REQUEST_URI} !^.*[^/]$
RewriteCond %{REQUEST_URI} !^.*//.*$
RewriteCond %{QUERY_STRING} !.*=.*
RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress|wp-postpass_).*$
RewriteCond %{DOCUMENT_ROOT}/wordpress/wp-content/cache/supercache/%{HTTP_HOST}/$1/index.html -f
RewriteRule ^(.*) /wordpress/wp-content/cache/supercache/%{HTTP_HOST}/$1/index.html [L]
# END WPSuperCache

All you need to do at that point is replace the two RewriteCond %{HTTP:Cookie} lines with the following:

RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress|wp-postpass_|YOURAPIKEY_user).*$

You can replace “YOURAPIKEY” with your Facebook application API key that you are using. If you are like the majority of the people that I read in the various forums I was reading, this should probably solve your problem. If it doesn’t then you are one of the unfortunate ones (like myself). Thankfully I have spent the past week pulling out my hair trying to build a convoluted system which ultimately resulted in 5 lines of code so you don’t have to.

Create a file called anything you’d like as long as it ends in .php. I called my file “fbconnect.php”. In that file place the following lines of code:

function fbc_supercache($cachestr){
   if(isset($_COOKIE[“YOURAPIKEY_user”]) )
      $cachestr .= $_COOKIE[“YOURAPIKEY_user”];
   return $cachestr;
add_cacheaction(‘wp_cache_get_cookies_values’, ‘fbc_supercache’);

Replace “YOURAPIKEY” with your actual API key and save it in the folder /wordpress/wp-plugins/wp-super-cache/plugins/. Upload that to your server and you should be good to go! Let us know if you run into any other problems. I only hope that you don’t have to experience the pain that I went through over the past week and that’s why I’ve gone ahead and posted this article!