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 Number of LinkedIn Shares With WordPress Shortcode

Count Number of LinkedIn Shares With WordPress Shortcode

There might be times when it's necessary to count the number of times a URL has been shared to LinkedIn. Used by website owners as a type of vanity stamp, and used by marketers as a basic means of determining popularity, the shortcode below will retrieve the count, cache the result for a defined period, then return the share count to your page.

Twitter carelessly removed their own (unofficial) count API in 2015, and Google recently disabled their own +1 feature (and associated count), so it may be only a matter of time before LinkedIn follows precedent and does the same.

The Result

Given that the LinkedIn count for our BeliefMedia articles is low (the site is new), we'll use the count for the same article on the now-retired Internoetics. The shortcode of [linkedinshares url="http://www.internoetics.com/2012/05/16/co ... gleplus/"] (snipped ). The result: 12.

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 Number of LinkedIn Shares With WordPress Shortcode
4
 http://www.beliefmedia.com/linkedin-shares
5
*/
6
 
7
function beliefmedia_linkedin_shares($atts) {
8
 
9
  $atts = shortcode_atts(array(
10
    'url' => '',
11
    'n' => 1,
12
    'cache' => 3600 * 12
13
  ), $atts);
14
 
15
 global $post;
16
 if ($atts['url'] == '') $atts['url'] = get_permalink($post->ID);
17
 
18
 $transient = 'bmlk_' . md5(serialize($atts));
19
 $cachedresult =  get_transient($transient);
20
 
21
 if ($cachedresult !== false ) {
22
  return $cachedresult;
23
 
24
  } else {
25
 
26
   /* Get LinkedIn Data */
27
   $json = file_get_contents('http://www.linkedin.com/countserv/count/share?url=' . $atts['url'] . '&format=json');
28
   if ($json !== false) $data = json_decode($json, true);
29
   $return = (!empty($data)) ? intval($data['count']) : 'Unavailable';
30
 
31
   /* Number format */
32
   if ($n) $return = number_format($return);
33
 
34
  set_transient($transient, $return, $atts['cache']);
35
  return $return;
36
 }
37
}
38
add_shortcode('linkedinshares', 'beliefmedia_linkedin_shares');

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 to query. If left blank, the current WP page will be used.

n

By default we'll return the number in a number format . Use n="0" or hardcode into the function to disable.

cache

The result is cached for 12 hours by default.

PHP Function

Used outside of WordPress, the following function may be used. Usage of Simple Cache is required.

1
<?php 
2
/*
3
 Count Number of LinkedIn Shares With WordPress Shortcode
4
 http://www.beliefmedia.com/linkedin-shares
5
*/
6
 
7
function beliefmedia_linkedin_shares($url, $cache = '43200') {
8
 
9
 $transient = 'bm_linkedin_' . md5($url . $cache);
10
 $cachedresult =  beliefmedia_get_transient($transient, $cache);
11
 
12
 if ($cachedresult !== false ) {
13
  return $cachedresult;
14
 
15
  } else {
16
 
17
   /* Get LinkedIn Data */
18
   $json = file_get_contents('http://www.linkedin.com/countserv/count/share?url=' . $url . '&format=json');
19
   if ($json !== false) $data = json_decode($json, true);
20
   $return = (!empty($data)) ? intval($data['count']) : 'Unavailable';
21
 
22
   /* Number format */
23
   $return = number_format($return);
24
 
25
  beliefmedia_set_transient($transient, $return);
26
  return $return;
27
 }
28
}
29
 
30
/* Usage */
31
echo beliefmedia_linkedin_shares($url = 'http://www.internoetics.com/2012/05/16/count-shares-to-facebook-twitter-linkedin-googleplus/');

Download


Title: LinkedIn Share Count in WordPress or PHP (WP Plugin)
Description: Count the number of LinkedIn shares with WordPress shortcode or PHP.
  Download • Version 0.2, 1.5K, zip, Category: WordPress Plugins (General)
WordPress Shortcodes, (707.0B)    PHP Code & Snippets, (687.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