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…

Two post types share the same taxonomy in WordPress

Sharing a WordPress taxonomy between two post types is useful especially if you want to create a relation between different…

November 14, 2019

Modify the language attribute based on category in WordPress

How to modify the language attribute in your Wordpress theme using a specific value

November 7, 2019

WordPress Fatal error: Unknown: Cannot use output buffering in output buffering display handlers in Unknown on line 0

How I've beat the php mysterious fatal error unknown in Wordpress caused by caching plugin WP Fastest Cache