Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wordpress function to get episode stats from Simplecast
<?php
/*
* This function can be used to pull total episode stats from Simplecast
*
*/
function cp_get_episode_stats( $podcast_id ) {
if ( ! $podcast_id) {
return "-";
}
$podcast = $podcast_id;
$url = 'https://api.simplecast.com/v1/podcasts/' . $podcast . '/statistics.json';
$api_key = 'REPLACE WITH YOUR API KEY';
$headers = array( 'X-API-KEY' => $api_key );
$request = wp_remote_get( $url, array( 'headers' => $headers ) );
if (is_wp_error($request)) {
return "-";
}
$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body );
$listens = $data->total_listens;
if ($listens > 999 && $listens <= 9999) {
$result = floor($listens / 1000) . 'K';
} elseif ($listens > 9999 && $listens <= 999999) {
$result = round($listens/1000, 1) . 'K';
} elseif ($listens > 999999) {
$result = floor($listens / 1000000) . 'M';
} else {
$result = $listens;
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.