In one of our previous posts “Custom Share button for Facebook” people have asked how to get counters value for Google +1 and Twitter for a custom URL.

So we decided to share this 3 simple WordPress snippets in this post, which output such social counters as Google +1, Twiter and Facebook for a custom URL.


Google +1 counter

Google doesn’t provide public API, so we need to make a little trick. We send JSON request to their button API.

function get_plusones($url) {
	
	$args = array(
			'method' => 'POST',
			'headers' => array(
				// setup content type to JSON 
				'Content-Type' => 'application/json'
			),
			// setup POST options to Google API
			'body' => json_encode(array(
				'method' => 'pos.plusones.get',
				'id' => 'p',
				'method' => 'pos.plusones.get',
				'jsonrpc' => '2.0',
				'key' => 'p',
				'apiVersion' => 'v1',
				'params' => array(
					'nolog'=>true,
					'id'=> $url,
					'source'=>'widget',
					'userId'=>'@viewer',
					'groupId'=>'@self'
			 	) 
			 )),
			 // disable checking SSL sertificates				
			'sslverify'=>false
		);
	
	// retrieves JSON with HTTP POST method for current URL	 
	$json_string = wp_remote_post("https://clients6.google.com/rpc", $args);
	
	if (is_wp_error($json_string)){
		// return zero if response is error								
		return "0";				
	} else {		
		$json = json_decode($json_string['body'], true);		 		 	
		// return count of Google +1 for requsted URL
		return intval( $json['result']['metadata']['globalCounts']['count'] ); 
	}
}


Twitter counter

Twitter has public API, so we can simply send request for a current URL, and get amount of tweets for this URL.

function get_tweets($url) {	

	// retrieves data with HTTP GET method for current URL		
	$json_string = wp_remote_get(
		'https://urls.api.twitter.com/1/urls/count.json?url='.$url,
		array(
			// disable checking SSL sertificates
			'sslverify'=>false
		)
	); 
	
	// retrives only body from previous HTTP GET request
	$json_string = wp_remote_retrieve_body($json_string);
	
	// convert body data to JSON format
	$json = json_decode($json_string, true);
	
	// return count of Tweets for requested URL		 	
	return intval( $json['count'] );
}


Facebook counter

With Facebook all as easy as with Twitter.

function get_shares($url) {	
	
	// retrieves data with HTTP GET method for current URL		
	$json_string = wp_remote_get(
		'https://graph.facebook.com/'.$url,
		array(
			// disable checking SSL sertificates
			'sslverify'=>false
		)
	);	
	
	// retrives only body from previous HTTP GET request	
	$json_string = wp_remote_retrieve_body($json_string);
	
	// convert body data to JSON format
	$json = json_decode($json_string, true);	
		
	if (isset($json['shares'])) {
		// return count of Facebook shares for requested URL
		return intval( $json['shares'] ); 
	} else {
		// return zero if response is error or current URL not shared yet
		return "0";
	}
}


Conclusion

You can place all of these functions in functions.php of your theme and then call them in templates.

I.E. like this:

//get permalinks of current post
$permalink = get_permalink($post->ID);

// show Google +1 for current post
_e('Google +1 counter - '.get_plusones($permalink));

// show Tweets for current post
_e('Tweets counter - '.get_tweets($permalink));

// show Facebook shares for current post
_e('Facebook shares counter - '.get_shares($permalink));



First part of this post with downloadable example: Custom Share button for Facebook

Don’t be shy to share this post if you found it useful.
Thank you.