RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.20% (5.24%*) • Home Loan Fixed: 5.48% (6.24%*) • Fixed: 5.48% (6.24%*) • Variable: 5.20% (5.24%*) • Investment IO: 5.78% (6.81%*) • Investment PI: 5.49% (6.32%*)

Count Facebook Page Likes with WordPress Shortcode or PHP

Count Facebook Page Likes with WordPress Shortcode or PHP

Note: With changes to Facebook's API, the code on this page may not work. We'll review it soon.

This article will show you how to include your Facebook page likes in plain text with WordPress shortcode, a PHP function, or with our WordPress plugin. This is the first of quite a few articles that'll show you how to include the counts of various social networks in your websites via various means. This function requires that you register a Facebook application (don't worry, it's easy). Follow the steps as described here.

As an example, the count for our Flight Facebook page, using the shortcode of [fblikes id="flightorg"], is Unavailable (bolding is mine). We've cached the result for 12 hours and applied number formatting by default (details below). The sad count for BeliefMedia's page is Unavailable (we're yet to promote it in any way).

WordPress Shortcode

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin. You may optionally download and install our plugin from the bottom of of the page.

1
<?php 
2
/*
3
 Count Facebook Page Likes with WordPress Shortcode or PHP
4
 http://www.beliefmedia.com/count-facebook-page-likes
5
*/
6
 
7
8
 
9
  $atts = shortcode_atts(array(
10
    'id' => 'flightorg',
11
    'accesstoken' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
12
    'appsecret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
13
    'n' => 1,
14
    'cache' => 3600 * 15,
15
    'cache_tmp' => 60 * 15,
16
  ), $atts);
17
 
18
 $transient = 'bmfbl_' . md5(serialize($atts));
19
 $cachedposts = get_transient($transient);
20
 
21
 if ($cachedposts !== false) {
22
 return $cachedposts;
23
 
24
  } else {
25
 
26
  /* App Secret Proof (if enabled) */
27
  $appsecret_proof = hash_hmac('sha256', $atts['accesstoken'], $atts['appsecret']);
28
 
29
  $json_url = 'https://graph.facebook.com/v2.9/' . $atts['id'] . '?fields=fan_count&access_token=' . $atts['accesstoken'] . '&appsecret_proof=' . $appsecret_proof;
30
  $json = @file_get_contents($json_url);
31
  if ($json !== false) $json_output = json_decode($json);
32
 
33
  if ($json_output->fan_count) {
34
 
35
   $likes = $json_output->fan_count;
36
   if ($atts['n']) $likes = number_format($likes);
37
   set_transient($transient, $likes, $atts['cache']);
38
   return $likes;
39
 
40
    } else {
41
 
42
   $likes = 'Unavailable';
43
   set_transient($transient, $likes, $atts['cache_tmp']);
44
   return $likes;
45
 
46
  }
47
 }
48
}
49
add_shortcode('fblikes','beliefmedia_facebook_page_likes');

If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.

WP Plugin

If you download our plugin you'll find the menu to update all options under Tools -> BeliefMedia Facebook Counts.

Count Facebook Page Likes with WordPress Shortcode or PHP

Find options at Tools → BeliefMedia Facebook Counts.
As of V0.3, the APP ID is replaced by an access token.

Any attributes supplied in your shortcode will overwrite the options saved to your database (normally just the Facebook page ID).

PHP Function

Used outside of WordPress, the following PHP function may be used. Usage requires Simple Cache (to cache data and avoid repeated and lengthy requests to Facebook).

1
<?php 
2
/*
3
 Count Facebook Page Likes with WordPress Shortcode or PHP
4
 http://www.beliefmedia.com/count-facebook-page-likes
5
 Requires Simple Cache: http://www.beliefmedia.com/simple-php-cache
6
*/
7
 
8
function beliefmedia_facebook_page_likes($id, $args = '') {
9
 
10
  $atts = array(
11
    'id' => 'xxxxxxxxxxxxx',
12
    'accesstoken' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
13
    'appsecret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
14
    'n' => 1,
15
    'cache' => 3600 * 13,
16
    'cache_tmp' => 60 * 16
17
  );
18
 
19
  /* Merge $args with $atts */
20
  $atts = (empty($args)) ? $atts : array_merge($atts, $args);
21
 
22
  $transient = 'fb_page_likes_' . md5(serialize($atts));
23
  $cachedposts = beliefmedia_get_transient($transient, $atts['cache']);
24
 
25
  /* App Secret Proof (if enabled) */
26
  $appsecret_proof = hash_hmac('sha256', $atts['accesstoken'], $atts['appsecret']);
27
 
28
  if ($cachedposts !== false) {
29
  return $cachedposts;
30
 
31
  } else {
32
 
33
  $json_url = 'https://graph.facebook.com/v2.9/' . $atts['id'] . '?fields=fan_count&access_token=' . $atts['accesstoken'] . '&appsecret_proof=' . $appsecret_proof;
34
  $json = @file_get_contents($json_url);
35
 
36
  if ($json === false) return 'Unavailable';
37
 
38
  /* Get data */
39
  $json_output = json_decode($json);
40
 
41
  if ($json_output->fan_count) {
42
 
43
   $likes = $json_output->fan_count;
44
   if ($atts['n']) $likes = number_format($likes);
45
   beliefmedia_set_transient($transient, $likes);
46
   return $likes;
47
 
48
  } else {
49
   $likes = '(Expired) ' . beliefmedia_get_transient_data($transient);
50
  }
51
 }
52
}
53
 
54
/* Usage */
55
echo beliefmedia_facebook_page_likes($id = 'flightorg');

Arguments are passed to the function in an $args array.

Considerations

  • You might consider rendering the result as an image. There are any number of ways of achieving this - notably with GD or ImageMagick. This very simple example renders each digit as an image.

Unavailable

  • Using our textbox shortcode you can include the count in a nicely formatted manner.

Join Unavailable others on our very lonely BeliefMedia FB page

or...

Join Unavailable others on our FB page for regular and free WP Code & Plugins.

  • If you download the plugin we'll provide you with update notifications via your dashboard. We expect to build lots of features into this plugin, such as the image feature described above.

Download


Title: Count Facebook Page Likes with WP Shortcode (Shortcode)
Description: Display a count of FB page likes in plain text with shortcode. Requires the creation of an application.
  Download • Version 0.3, 796.0B, zip, Category: WordPress Shortcodes
WordPress Plugins (General), (4.5K)    PHP Code & Snippets, (894.0B)    

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

  E. Australia Standard Time [ UTC+10, Default ] [ CHECK TO CHANGE ]

  Want to have a chat?
 

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment