Facebook: WordPress code to display the number of comments / shares / likes for the current blog post for administrators only

Hi,

Let’s say you have a Facebook page and you want to know how many likes / shares / comments does the current blog post has. I made a script, based on this article, which allows you so see how many likes / shares / comments a blog post has.

It will give you an information like this:

sharesvslikes

How to do it? You should add the following code:

if( current_user_can('administrator') ) {

$source_url = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$rest_url = "http://api.facebook.com/restserver.php?method=links.getStats&urls=".urlencode($source_url);
$xml_response = file_get_contents($rest_url);
$xml_record = simplexml_load_string($xml_response);
$fb_share_count = $xml_record->link_stat->share_count;
$fb_like_count = $xml_record->link_stat->like_count;
$fb_comment_count = $xml_record->link_stat->comment_count;
$fb_total = $xml_record->link_stat->total_count;
echo '<br/><br/><b>Administrator</b> information:<br/>';
echo 'Facebook Shares: '.$fb_share_count.'<br/>';
echo 'Facebook Likes: '.$fb_like_count .'<br/>';
echo 'Facebook Comments: '.$fb_comment_count .'<br/>';

in your single.php file (or wherever else you need it), to have it display the information as in the picture above.

Change the condition to

if( (current_user_can('administrator')) && (is_single()) ) 

, if you want to add the code to only launch if the current page is single (so, you can thus put the code in header.php or in footer.php or in sidebar.php).

I am a Digital Marketing Manager for The KPI Institute. My expertise is in SEO (Search Engine Optimization) / UX (user experience) / WordPress. Co-founder of lumeaseoppc.ro (series of events on SEO & PPC) and cetd.ro (Book on branding for MDs). On a personal level, I like self-development - events, sports, healthy living, volunteering, reading, watching movies, listening to music.

No comments yet.

Leave a comment

Your email address will not be published.