Displaying Facebook Post Share Count in Plain Text

Back in June 2014, I wrote a post a where I have shown how to grab the Facebook page like count of any given Facebook page and display them in Plain text. Today, in this post I will show you how to grab the Post share count which is little bit different from getting the Facebook Page like count.

We would be using pretty much the same function like before but there would be little twist to make it dynamic. What I meant by that is, while you are getting Facebook page like count, you are grabbing the like count of an static URL. For individual page or posts, we need to grab the permalink every time we want retrieve the share count. Makes sense? Don’t worry, let’s get started.

Here is the function that you should be looking for.

<?php
function facebook_share_count($id) {
   $pid = get_permalink($id);
   $url = 'https://graph.facebook.com/?id='.$pid;
   $content = wp_remote_retrieve_body(wp_remote_get($url));
   if( is_wp_error($content)) { return 0; } 
   $find = preg_match('%shares":\K\d %', $content, $matches);
   if (!$find == '') { echo $matches[0]; } else { echo '0';} } 
?>

So, this is our main function that you need to place on your theme’s functions.php file and update the file. Now, I will discuss this function within a minute but before anything else, you need to add the following line on your single.php or page.php file. You can use this function for custom post type (i.e. single-cpt.php) as well.

<?php 
if (function_exists('facebook_share_count')) { 
   echo facebook_share_count(get_the_ID()); } 
?>

Explanation:

On our main function, I used $id as an input variable that would grab the post of page ID number that needs to be checked for share count. The $pid variable stores the permalink of that ID (post/page) and hands it over to $url variable to check. I have used the get_permalink WordPress function for this purpose.

I also used, Facbook Graph URL to grab the data about this specific url. It should look like this:

https://graph.facebook.com/?id=https://www.test.com/

So, basically all I am doing here is to provide the full permalink of the post or page and retrieving the result that gets displayed after the https request.

I have also used $content variable to grab the content that gets displayed right after a https:// request. Feel free to check out wp_remote_retrieve_body function for better understanding. WordPress has a very healthy codex page on this.

On sixth line, I checked if there is any error on the returned response. If there is any, it would return “0”. If there is no error, it would move on to look for possible keyword (shares) match and extract the number right after the word or count number in other word.

Today In History

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from Postable, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.