3 Ways of Getting Shares for URL using Facebook 3.0 API
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.

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:

4. The last step is to make your 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( add_query_arg( array(
'id' => urlencode( get_permalink( $post_id ) ),
'access_token' => $access_token,
'fields' => 'engagement'
), 'https://graph.facebook.com/v3.0/' ) );
$body = json_decode( $response['body'] );
//print_r($body);
$count = intval( $body->engagement->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 );
$body->engagement
object also containsreaction_count
,comment_count
andcomment_plugin_count
data.
cURL example
Function:
/**
* 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/v3.0/?id=' . urlencode( $url ) . '&fields=engagement&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->engagement->share_count );
}
Usage:
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';
$.ajax({
url: 'https://graph.facebook.com/v3.0/',
dataType: 'jsonp',
type: 'GET',
data: {fields: 'engagement', access_token: token, id: url},
success: function(data){
console.log(data);
$('body').append(data.engagement.share_count);
},
error: function(data){
console.log(data); // send the error notifications to console
}
});

Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
Thanks, friend.
thanks a lot
I spend an entire day looking for this
I’m glad to help! You’re welcome :)
Thanks! This is what I looking for since facebook start to use API 2.7
I can’t share this post on Facebook with your share button. It says: App Not Setup: This app is still in development mode, and you don’t have access to it. Switch to a registered test user or ask an app admin for permissions.
Thank you very much for your comment. Please, try now.
Sorry, but this doesn’t get the share count. It actually gets “likes, shares and comments” combined.
It’s a shame facebook no longer allows you to get each individual #.
How without Access Token?
Url for example:
can you input a wp code without saving to database? it creates a huge load on the server
thanks
Hi Mike,
do you mean
set_transient()
? Are you sure about huge load?Anyway, if you do not want to use cache, you can remove lines 7, 9,12,18,19
Thank you this is exactly what I needed!
Thanks, It works!
Hi,
Thanks for the code. I have tried to add my own APP ID|APP SECRET, but in the returned array there are no share infos.
Array with my access_token:
stdClass Object ( [og_object] => stdClass Object ( [id] => 1511323895597932 [description] => Loaves on loaves on loaves [title] => The Great British Baking Show Episode 3: Bread! [type] => article [updated_time] => 2017-06-26T19:55:46+0000 ) [id] => https://food52.com/blog/19948-the-great-british-baking-show-episode-3-bread )
Array with yours access_token:
stdClass Object ( [share] => stdClass Object ( [comment_count] => 0 [share_count] => 236 ) [id] => https://food52.com/blog/19948-the-great-british-baking-show-episode-3-bread )
How can I use my access_token to get share infos?
Thanks,
Peter
Hi Peter,
I’m not sure why this happens, I’ve just tested it with my different apps and every time I got the correct number of shares
How could I save the number as meta (and updated now and then)?
Hello,
Just below the line 18 in the first code example add something like this:
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 usageecho 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…
If nothing happend - 0 times.
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.
Hi Don,
Haha, of course your have to use your own App Credentials 😁
how to do it without input btn?
You can get the value from
<input type="hidden">
as an option.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.
Hi,
Use PHP example in this case.
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.
Hey,
All APP settings are described in this tutorial
Hey Misha, im not able to get share count, might be i need have some permission to my fb app. Let me know. i’m refering to v3.0.
Hey Vijay,
I’ve just updated the tutorial according to the new Facebook 3.0 API 🎉
List of changes:
1. The API endpoint has been changed from
https://graph.facebook.com/v2.7/
tohttps://graph.facebook.com/v3.0/
.2.
fields=engagement
parameter has been added to the request3. Share count in response was in
share
object, now it is inengagement
object.Cool,its working. Thanks!
Hi Misha, is there any way to do this in Google Spreadsheets?
Hi,
Thank you for the good idea for my new tutorial 🙃
Hey there,
Aren’t we not supposed to use the “App Secret Key” in the js stuff, this Secret Key being… secret ?
Thx for your concern
Hey,
Depends on a situation. You can use the cURL example if you do not want your secret key to be visible.
What about API usage limitations
we are reaching them using our token in our site pages. Is there a way to overcome it?
error: {
message: “(#4) Application request limit reached”,
type: “OAuthException”,
is_transient: true,
code: 4,
fbtrace_id: “GJeaudmp+zZ”
}
Hey,
Do you use cache transients?
Hi Misha,
Is there a way to use jQuery to get the comment count without having to submit the URL through a form? I want to be able display the comment count on my homepage teasers of how many FB comments are on the post when the user clicks through. I am not using WordPress, otherwise I just use #1.
Hi Colin,
All you need is in the tutorial, just replace this line at this step.
Thanks Misha! This helps a ton.
Hi Misha
Thank you very much for your guide! I have been using https://graph.facebook.com/URL but that has a limit of requests. I did not find (or understand) the information in Facebook Developer how to do make a simple app to get the number of likes for a page (I do not want the like button because of the tracking).
Your guide did the trick. One thing, though; using graph is giving the total of reactions, comments and share, thus the number returned using your method is lower. For other non programmers that are looking to get the higher number using cURL: You can change the last two lines in the cURL example to:
Hi Kim,
Thank you for your comment! 🙃
Hi Misha,
I tried this a few times and couldn’t get it to work … but in the end, success. Thanks so much for this.
That awsome !. thanks you so much .
Hello,
It doesn’t seem to cover all users on facebook – it only counts engagement from users who are connected to my developer account’s facebook profile. Is there a way to make it cover all users/posts on facebook?
Thank you,
Christian