Get Google Plus Follower count from PHP in WordPress

I’ve found a similar function to retrieve the number of followers of a Google Plus page, inside WordPress, but it…

May 7, 2014

I’ve found a similar function to retrieve the number of followers of a Google Plus page, inside WordPress, but it didn’t work. There was an error in the preg_match function, probably because the html code of Google Plus has changed. So, here is my new fixed version, which parse Google Plus pages and doesn’t use any allowed API:
Put this function in your functions.php theme file and retrieve google plus followers count wherever you like in your theme’s files.

function getGplusFollowers($url){
	$count = get_transient('gplus_count');
	if ($count !== false) return $count;
	$count = 0;
	$data = file_get_contents($url);
	if (is_wp_error($data)) {
		return '!';
	} else {
		if (preg_match("#([0-9,\.]*)</span>(\s+)?follower#Uis", $data, $matches)) {
			$results =  preg_replace("/[,\.]/", '', $matches[1]);
		}
		if ( isset ( $results ) && !empty ( $results ) ) {
			$count = $results;
		}
	}
	set_transient('gplus_count', $count, 60*60*48); // 72 hour cache
	return $count;
}

Author

I'm a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I'm the founder of Rockit.it and Dailybest.it and I'm actually the Chief Technical Officer of Better Days web agency.

Recommended

Top Social Stories plugin and widget for WordPress

Just released a plugin for WordPress that can be used to track your posts on Facebook, Twitter and Google+. When…

November 3, 2013

Social buttons: the fastest way for WordPress, without plugins

NOTE: the code in this post is written for WordPress but you can easily translate it in any language. You’re here…

September 15, 2015

Optimize WordPress, a long list of tips

In the above image you can see your WordPress before reading this post, and after the optimizations you will make…

A new avatar generator for WordPress with get_avatar and Flathash

Brand new avatars for WordPress Gravatar service

April 7, 2017

3 ways to add author’s image in WordPress

Add author's image in WordPress without a plugin

April 5, 2017

Stop sending email to new user in WordPress admin registration

Prevent WordPress to send notification emails when you manually create new users

March 9, 2017