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 Shares in Plain Text With WordPress Shortcode or PHP

Count Facebook Shares in Plain Text With WordPress Shortcode or PHP

Facebook has always remained relatively open with regard to the access it provides to their (or your) data - either by way of a registered API call or an open graph request. This article will show you how to include the Facebook share count (via unregistered graph access) into a WordPress post or page, or into a PHP application, in plain text.

Note: This hack has reached its end of life. Always use the Facebook API when able. Details here. Article remains for information purposes only.

For the example, we'll reference the article on Internoetics where the code was first published. The shortcode of [fbcount url="http://www.internoetic ... inkedin-googleplus/"] (snipped ) returns a count of N/A.

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 Shares in Plain Text With WordPress Shortcode or PHP
4
 http://www.beliefmedia.com/count-facebook-shares
5
*/
6
 
7
8
 
9
  $atts = shortcode_atts(array(
10
    'url' => '',
11
    'n' => 1,
12
    'cache' => 3600 * 12
13
  ), $atts);
14
 
15
 global $post;
16
 
17
 /* Get URL */
18
 if ($atts['url'] == '') $atts['url'] = get_permalink($post->ID);
19
 
20
 $transient = 'bmfsc_' . md5(serialize($atts));
21
 $cachedresult =  get_transient($transient);
22
 
23
 if ($cachedresult !== false ) {
24
  return $cachedresult;
25
 
26
  } else {
27
 
28
   $json = file_get_contents('http://graph.facebook.com/?ids=' . $atts['url']);
29
   if ($json !== false) $data = json_decode($json, true);
30
   else return 'Unavailable';
31
 
32
   $url = $atts['url'];
33
   $return = intval( $data["$url"]['share']['share_count'] );
34
 
35
   if ($atts['n']) $return = number_format($return);
36
 
37
  set_transient($transient, $return, $atts['cache']);
38
  return $return;
39
 }
40
}
41
add_shortcode('fbsharecount', 'beliefmedia_facebook_share_count');

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.

Shortcode Attributes

url

The url of the page you're querying for a count. If left blank the current page will be used.

n

The result is returned in a number_format by default. To disable, use n="0".

cache

The period for which results are cached. By default results are cached for 12 hours.

PHP Function

Used outside of WordPress the following function may be used. Usage requires Simple Cache.

1
<?php 
2
/*
3
 Count Facebook Shares in Plain Text With WordPress Shortcode or PHP
4
 http://www.beliefmedia.com/count-facebook-shares
5
*/
6
 
7
function beliefmedia_facebook_share_count($url, $cache = '43200') {
8
 
9
 if ($url == '') return 'Require URL';
10
 
11
 $transient = 'bm_facebookcount_' . md5($url . $cache);
12
 $cachedresult =  beliefmedia_get_transient($transient, $cache);
13
 
14
 if ($cachedresult !== false ) {
15
  return $cachedresult;
16
 
17
  } else {
18
 
19
   $json = file_get_contents('http://graph.facebook.com/?ids=' . $url);
20
   if ($json !== false) $data = json_decode($json, true);
21
   else return 'Unavailable';
22
 
23
   /* Get count value */
24
   $return = number_format(intval( $data["$url"]['share']['share_count']));
25
 
26
  beliefmedia_set_transient($transient, $return);
27
  return $return;
28
 }
29
}
30
 
31
/* Usage */
32
$url = 'http://www.internoetics.com/2012/05/16/count-shares-to-facebook-twitter-linkedin-googleplus/';
33

Considerations

  • The returned JSON data includes other information that may be of use (including the last update time). The title, description, and other data are sourced from your Object Graph tags. The array below is the response returned for our example URL.
1
Array
2
(
3
    [http://www.internoetics.com/2012/05/16/count-shares-to-facebook-twitter-linkedin-googleplus/] => Array
4
        (
5
            [share] => Array
6
                (
7
                    [comment_count] => 0
8
                    [share_count] => 59
9
                )
10
 
11
            [og_object] => Array
12
                (
13
                    [id] => 10150947495748304
14
                    [description] => Tweet I've recently consolidated two websites into as a means of managing my time and resources more effectively. The other sites will be revive...
15
                    [title] => Count the Number of Shares to Facebook, Twitter, LinkedIn & Google Plus
16
                    [type] => article
17
                    [updated_time] => 2016-08-25T08:38:28+0000
18
                )
19
 
20
            [id] => http://www.internoetics.com/2012/05/16/count-shares-to-facebook-twitter-linkedin-googleplus/
21
        )
22
 
23
)

Download

No longer available.

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