Function Code:
/* Helper function */
function cp_get_share_count( $type, $current_page ) {
$share_array = array('facebook', 'google', 'linkedin', 'pinterest');
$sec_Array = array( 'reddit', 'stumbleupon' );
$count_name = '';
$count_data = '';
$count_url = '';
$count = 0;
$count_name = strtolower( $type );
if (in_array( $count_name, $share_array)){
$count_url = "https://count.donreach.com/?url=".$current_page;
}else if(in_array( $count_name, $sec_Array)){
$count_url = "http://share-count.appspot.com/?url=".$current_page;
}else{
//exit();
}
$count_response = wp_remote_get( sprintf( $count_url ) );
if( is_wp_error( $count_response ) or ( wp_remote_retrieve_response_code( $count_response ) != 200 ) ) {
return false;
}
$count_data = json_decode( wp_remote_retrieve_body( $count_response ), true );
if ( !is_array( $count_data ) ) {
return false;
}
$shares = $count_data['shares'];
foreach ($shares as $key => $value) {
if( $count_name == $key ){
$count = $value ;
}
}
return $count;
}
How to use?
/*--------------------------------------------------------*/
/*
*$type = 'facebook' i.e name of network
*$current_page = "URL for page"
*/
cp_get_share_count($type, $current_page); //call function
Not the solution you are looking for? Check other
articles, or open a
support ticket.