Created
May 10, 2011 18:18
-
-
Save dalethedeveloper/965036 to your computer and use it in GitHub Desktop.
Wordpress + Facebook Done Right: Load FBConnect asynchronously for XFBML and handle the channelUrl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A class-based approach to bring in Facbook Connect asynchronously on every | |
* frontend page load. Also handles side-loading channelURL when using | |
* Facebook's FB.init() method. | |
* | |
* This fits nicely in any functions.php, or can be dropped in to a standalone | |
* php file to use as a plugin. | |
* | |
* @link http://developers.facebook.com/docs/reference/javascript/FB.init/ | |
*/ | |
if( !class_exists('go_fbconnect') ) { | |
class go_fbconnect { | |
public function __construct() { | |
add_filter('rewrite_rules_array', array($this,'fb_rewrite') ); | |
if( !is_admin() ) { | |
add_action('wp_head', array($this,'add_fbconnect'), 15); | |
add_action('wp_enqueue_scripts', array($this,'add_jquery')); | |
add_filter('query_vars', array($this,'fb_queryvars') ); | |
add_filter('wp_headers', array($this,'output_channel') ); | |
} | |
} | |
/** | |
* Be very sure we enqueue jQuery, as we need it in add_fbconnect() | |
* | |
* Action: wp_enqueue_scripts | |
*/ | |
public function add_jquery() { | |
wp_enqueue_script('jquery'); | |
} | |
/** | |
* Force asynchronous addition of the FB Connect javascript | |
* | |
* Action: wp_head | |
*/ | |
public function add_fbconnect() { | |
$site_url = get_bloginfo('url'); | |
echo <<<FB | |
<script type="text/javascript"> | |
(function() { | |
jQuery(document).ready(function() { | |
jQuery.getScript('http://connect.facebook.net/en_US/all.js#xfbml=1', function(){ | |
FB.init({ | |
xfbml: true, | |
channelUrl: '{$site_url}/channel.html' | |
}); | |
}); | |
}); | |
}()); | |
</script> | |
FB; | |
} | |
/** | |
* Add a query var as a flag for channel.html | |
* | |
* Filter: query_vars | |
*/ | |
public function fb_queryvars($qvars) { | |
$qvars[] = 'fb_channel'; | |
return $qvars; | |
} | |
/** | |
* Add channel.html to our rewrites | |
* | |
* Filter: rewrite_rules_array | |
*/ | |
public function fb_rewrite($wprw = array()) { | |
return array('channel.html.?$' => 'index.php?fb_channel=1') + $wprw; | |
} | |
/** | |
* If WP has been called with our rewrite to /channel.html.? | |
* simply output a permacached script inclusion and die. | |
*/ | |
public function output_channel($headers) { | |
global $wp; | |
if( !empty($wp->query_vars) and isset($wp->query_vars['fb_channel']) and $wp->query_vars['fb_channel'] ) { | |
if(ini_get('zlib.output_compression')) | |
ini_set('zlib.output_compression', 'Off'); | |
@header('Expires: Wed, 1 Jan 2020 05:00:00 GMT'); | |
@header('Cache-Control: max-age=290304000,public'); | |
@header('Pragma: public'); | |
@header('Content-Type: text/html; charset=UTF-8'); | |
// Shouldn't be needed: '<script>document.domain = "mySite.com";</script>' | |
die('<script src="http://connect.facebook.net/en_US/all.js"></script>'); | |
} | |
} | |
} | |
$go_fbconnect = new go_fbconnect(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment