WordPress
How to count facebook page likes in WordPress
11 August, 2015
0

Maybe your’e looking for a facebook likes counter for your WordPress blog. There are some premium and free plugins exist to do this. But today I’ll show you how can you do this without any plugin using a simple function.

First of all, go to your theme folder and open functions.php file. Then paste the following function into it.

function get_facebook_likes($page_id) {
	$app_id = 'Your facebook app id here...';
	$app_secret = 'Your facebook app secret here...';

	if (!empty($app_id) && !empty($app_secret)) {
		$access_token = $app_id . '|' . $app_secret;
	}

	if (!empty($access_token) && !empty($page_id)) {
		$response = wp_remote_get('https://graph.facebook.com/' . $page_id . '?access_token=' . $access_token);
		$response = wp_remote_retrieve_body($response);

		if (is_wp_error($response)) {
			$total = 0;
		} else {
			$_data = json_decode($response, true);
			$total = intval($_data['likes']);
		}

		return $total;
	}
}

Now you need to change app_id and app_secret with your own. To register a new app, please visit Facebook Developers page and register your new apps.

Finally, call the function where you want to show the counter. Pass your page id as argument.

echo get_facebook_likes('Your page id');

There is no style included with it. Design your own way.

5

About author

Sabuz

Related items

/ You may check this items as well

Ajax media upload in WordPress

Maybe your’e looking for a facebook likes counte...

Read more