Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggreagtion Enquires Welcome

Add The Facebook SDK To WordPress

If you plan on adding any kind of Facebook functionality to your website, such as like buttons, integrated videos, comments, or a page plugin, you will be required to add a snippet of JavaScript to each and every page you would like to see those features. While there's often an iframe option to accompany many of the features, the SDK option is almost always a better fit.

In the past, we always included the SDK JavaScript code with any of the code we shared, but this option meant that we were often providing snippets of code over and over again - often with minor variances. Providing a single source of the function will make it easier for us to share Facebook shortcodes for WordPress (and other PHP snippets) in the future.

While we've provided a WordPress plugin, it's almost always easier to simply add the code snippet to your functions.php file (assuming you're using one).

The WordPress Function

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

1
<?php 
2
/*
3
 Add The Facebook SDK To WordPress
4
 http://www.beliefmedia.com/facebook-sdk-wordpress
5
*/
6
 
7
function beliefmedia_fbsdk_head() {
8
 
9
  $version='2.9';
10
  $appid='';
11
 
12
  if ($version) $opts = '&version=v' . $version;
13
  if ($appid) $opts .= '&appId=' . $appid;
14
 
15
?>
16
 
17
  <div id=&quot;fb-root&quot;></div>
18
  <script>(function(d, s, id) {
19
    var js, fjs = d.getElementsByTagName(s)[0];
20
    if (d.getElementById(id)) return;
21
    js = d.createElement(s); js.id = id;
22
    js.src = &quot;//connect.facebook.net/en_US/sdk.js#xfbml=1<?php echo $opts; ?>&quot;;
23
    fjs.parentNode.insertBefore(js, fjs);
24
  } (document, 'script', 'facebook-jssdk'));</script>
25
 
26
<?php
27
}
28
add_action( 'wp_head', 'beliefmedia_fbsdk_head' );

While we can pass arguments to a function when using the add_action function (through do_action ), it's not appropriate with wp_head (no arguments are called by wp_head()). While we can use apply_filters to modify values, it's just easier to just define the values - which is what we've done (for $version and $appid).

PHP Function

Used outside of WordPress, the beliefmedia_fbsdk_head() function should be used just after the opening body tag.

1
<?php 
2
/*
3
 Add The Facebook SDK To WordPress
4
 http://www.beliefmedia.com/facebook-sdk-wordpress
5
*/
6
 
7
function beliefmedia_fbsdk_head($version='2.9', $appid='') {
8
 
9
  if ($version) $opts = '&version=v' . $version;
10
  if ($appid) $opts .= '&appId=' . $appid;
11
 
12
  $js = '<div id=&quot;fb-root&quot;></div>
13
  <script>(function(d, s, id) {
14
    var js, fjs = d.getElementsByTagName(s)[0];
15
    if (d.getElementById(id)) return;
16
    js = d.createElement(s); js.id = id;
17
    js.src = &quot;//connect.facebook.net/en_US/sdk.js#xfbml=1' . $opts . '&quot;;
18
    fjs.parentNode.insertBefore(js, fjs);
19
  } (document, \'script\', \'facebook-jssdk\'));</script>';
20
 
21
 echo $js;
22
}
23
 
24
/* Usage (after opening body tag) */
25
beliefmedia_fbsdk_head();

Considerations

  • It's quite possible your theme or another plugin is already rendering the Facebook SDK code. To check, view your website source code and look for "connect.facebook.net". If the code exists, it's highly likely the code already exists.
  • If you download the WP plugin, we'll notify you of updates via your dashboard.

Video Introduction

Download

The WordPress version intended for your functions.php file is labeled as shortcode.


Title: Add The Facebook SDK To WordPress (WP Plugin)
Description: Add The Facebook SDK To WordPress. It is required for a number of other plugins and snippets.
  Download • Version 0.2, 1.5K, zip, Category: WordPress Plugins (General)
WordPress Shortcodes, (595.0B)    PHP Code & Snippets, (589.0B)    

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  AUS Eastern Standard Time (Washington)

  Want to have a chat?
 

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment

READY TO HAVE A CHAT? CALL US ANYTIME ON 1300 235 433