Displaying Facebook Like Count in Plain Text

Over the years, we have learned that displaying Facebook like box on any website is just one of the way to engage with your visitors. This also helps visitors to like your official Facebook page and get the latest updates from your site. However, it is also true that adding Facebook like box or Like Button makes your web page little slower as these plugin takes time to load the necessary files to display the box and like count.

This problem can easily be solved by simply adding a CSS driven button of box along with displaying the like count. In this post, I will show you how to retrieve the Facebook page like count and display them as text. So, lets get started.

function facebook_like_count() {
   $pageid = 'xxxxxxxxxxxxxxxx';
   $url = 'https://graph.facebook.com/'. urlencode($pageid);
   $content = wp_remote_retrieve_body(wp_remote_request($url));
     if (is_wp_error($content)) {
	return 0; }
   $decode = json_decode($content);
   $value = intval($decode->likes);
   return $value; }

If you want to use this code on your site, simply copy and paste the code on your theme’s functions.php file and update the page. Do not forget to change value of $pageid variable with your Facebook page ID number.

Once you are done, simply call this function anywhere on your theme’s pages. It would display the Facebook page like count. This is how you should call this function.

echo facebook_like_count();

That’s it. All you have to do now is to call this function wherever you want to style or to display the count.

How to Grab the Facebook Page ID

In case if you don’t know how to grab your Facebook page ID, this is what you need to do.
Step 1: Log in to your Facebook account and visit your Facebook page.
Step 2: Take a look at the top of the page and click on “Settings”. A new page should appear.
Step 3: Click on “Page Info” link from the left hand side bar.

Scroll down to the bottom of the page (Page Info). You should find your Facebook page ID.

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.