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…

Maggio 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

PHP expert. Wordpress plugin and theme developer. Father, Maker, Arduino and ESP8266 enthusiast.