Fetching Simplecast stats with Wordpress

Cory Pratt

A recent project has required pulling live podcast related information from Simplecast to be displayed for various podcast. Thankfully Simplecast has a relatively simple API that can be used with some simple Wordpress functions.

Get your API Key

Before using the code below, you need to get your API key from your dashboard. Save this key somewhere safe you can refer to it later.

Find your Podcast ID

Each podcast you create in Simplecast will get a unique ID. I have found the easiest way to get your podcast's ID is to manage your podcast in your dashboard and find the ID in the URL. You should see something like:

https://simplecast.com/podcasts/XXXX/episodes

Where the XXXX you should see a string of numbers for your podcast. Hang on to that.

The Function

I've saved this function in a Core Functionality plugin, but it can also easily live in your functions.php file. The function takes one argument, the podcast ID you retrieved previously. Make sure you save your API key on line 14.

The first quick check is to see if there is a podcast id passed, if not the function bails quickly and simply returns a "-".

The function uses the Wordpress native functions wp_remote_get and wp_remote_retrieve_body to grab the results from Simplecast and do a quick parse of the results to get the 'total_listens' data.

Lines 28-36 do a little fancier formatting to display the numbers a little nicer, feel free to edit to your liking.

From this point, you can call the function in your theme using the following:

<?php echo cp_get_episode_stats($simplecast_id); ?>

The code could also be extended to have the data stored in the database as transient data as well as called from a shortcode.

Simplecast has other pieces of information that can be pulled from their API. You can use the same function above, just adjust the URL and get the data from the appropriate results.