3 Ways of Getting Shares for URL using Facebook 2.7 API

In this tutorial I will show you 3 examples of getting Facebook shares using the new Graph API – cURL PHP example, WordPress HTTP API example and only jQuery example.

#Facebook, #facebook, #httpapi  /   28

No matter what way you choose, in any case you should make a little preparation — create a Facebook App and get Access Token from it (App ID|App Secret).

Step 1. Creating Facebook App #

1. Go to Facebook Developers page and click Add a New App link.

Facebook Applications List. Ann a new app.

2. After clicking that link a popup will appear. Input any App Name, your Contact Email and select Category.

3. Get App ID and App Secret on this page:

How to get Facebook Application ID and Secret

4. The last step is to make your facebook application public.

How to make Facebook Application Public

So, that’s it, in all examples in this post our Access Token will look like this: Application ID|App Secret.

Step 2. Get Shares Count. Ready-to-use Examples. #

WordPress, cURL and jQuery examples are below.

WordPress HTTP API example #

If you are not familiar with WordPress, insert the following code to the functions.php file of your active theme.

 * Display number of shares using WordPress HTTP API
 * @param integer $post_id We want to get number of shares of the post with this ID
function wp_get_shares( $post_id ) {
	$cache_key = 'misha_share' . $post_id;
	$access_token = 'APP_ID|APP_SECRET';
	$count = get_transient( $cache_key ); // try to get value from Wordpress cache
	// if no value in the cache
	if ( $count === false ) {
		$response = wp_remote_get('https://graph.facebook.com/v2.7/?id=' . urlencode( get_permalink( $post_id ) ) . '&access_token=' . $access_token );
		$body = json_decode( $response['body'] );
		$count = intval( $body->share->share_count );
		set_transient( $cache_key, $count, 3600 ); // store value in cache for a 1 hour
	return $count;

If you add this function to your functions.php you can now freely use it anywhere in your theme files like this:

echo wp_get_shares( 355 );

cURL example #


 * Display number of shares using PHP cURL library
 * @param string $url ID We want to get number of shares of this URL
function curl_get_shares( $url ){
	$access_token = 'APP ID|APP SECRET';
	$api_url = 'https://graph.facebook.com/v2.7/?id=' . urlencode( $url ) . '&access_token=' . $access_token;
	$fb_connect = curl_init(); // initializing
	curl_setopt( $fb_connect, CURLOPT_URL, $api_url );
	curl_setopt( $fb_connect, CURLOPT_RETURNTRANSFER, 1 ); // return the result, do not print
	curl_setopt( $fb_connect, CURLOPT_TIMEOUT, 20 );
	$json_return = curl_exec( $fb_connect ); // connect and get json data
	curl_close( $fb_connect ); // close connection
	$body = json_decode( $json_return );
	return intval( $body->share->share_count );


echo curl_get_shares( 'https://rudrastyh.com/api/get-facebook-shares.html' );

jQuery example with live demo #

Well, I hope you know what is jQuery and how to work with it. Here is the code and there is a live demo below.

var token = 'APP ID|APP SECRET', // learn how to obtain it above
url = 'YOUR URL';
	url: 'https://graph.facebook.com/v2.7/',
	dataType: 'jsonp',
	type: 'GET',
	data: {access_token: token, id: url},
	success: function(data){
	error: function(data){
		console.log(data); // send the error notifications to console

See the Pen grVGvk by Misha (@rudrastyh) on CodePen.light

Only the best of WordPress

Subscribe to this weekly newsletter to receive the latest blog posts by email.I respect your privacy. Your email is safe with me.

Comments 28

← Older
  • How could I save the number as meta (and updated now and then)?

    • MishaAuthor July 7, 2017 at 21:40


      Just below the line 18 in the first code example add something like this:

      update_post_meta( $post_id, 'facebook_share_count', $count );
  • There is no data across the Internet on the subject. The only instruction that appears to be working is yours. But not everyone can code. Thus, any insignificant mistake does matter.

    How to call this f***g function in WordPress???

    You wrote “echo wp_get_shares( 355 );” What is this 355??!?!??!?! I need to implement it with a permalink. Why you didn’t write that I need to use to call this function??!??!

    All I need is just to display the number (quantity) of shares, for example, 5 shares, or 3 shares, or 4595059 shares.

    What string do I need to write in my single.php to force this code to work?

    What is 355?!?!?! Why not 484????????

    doesn’t work as well
    gives me the same result?

    How can I call this function to any wordpress post???? Give me the EXACT string, please.

    • Ahaha,

      Everything is in this post 😈 read it one more time, then one more time, then one more time again!!!!!

      P.S. 355 is the ID of WordPress post, the function itself (begins with function…) insert to your current theme functions.php, but the function usage echo wp_get_shares( post_id ) to the place where you would like to display share count.

  • Hi Misha, i try your code… but i don’t know…its not working to me. i create a drop in my website: http://webbatlas.com/test-beta/ Please check its not working.. any idea

  • Below is the code in html…

    <form id="get_shares">
      <input type="text" id="url" placeholder="Input URL" size="50" />
      <button>Get shares</button>
    <div id="results">http:// or https://<br />&nbsp;<br />If nothing happend - 0 times.</div>
    <script type="text/javascript">
    jQuery(function($) {
      var token = '666987483466439|b3e1a0948513223b0bc51b32a735e2cf';
          url: 'https://graph.facebook.com/v2.1/',
    	    dataType: 'jsonp',
    	    type: 'GET',
    	    data: {access_token: token, id: $('#url').val() },
    	    success: function(data){
     		    $('#results').html('<strong>Number of shares:</strong> ' + data.share.share_count);
        return false;
  • I made it work now…

    But, Using my own App ID and App Secret. Its not working. I also made it public..same in your example.

  • how to do it without input btn?

  • vikas shishodia January 9, 2018 at 10:10

    Hi @misha,
    This is giving me the count of share+likes+comments, however i need only share counts.

    Can you please guide me on this that how to get only share counts.

  • I replaced the access token with my app Id and app Secret. But it is not returning me the share count.Is there any setting to do with my app? I have used the Jquery and Html code.

Leave your question or feedback

Please, enter a comment
Please, enter a name
Incorrect email