On Monday we provided the first of a few variants of shortcode that would render a SoundCloud audio player in your WordPress website. This article will provide code that'll periodically mine SoundCloud for a play count, download count, comment count, or like count. Because we're scraping the freely provided meta data, use of the API isn't required.
Note: Soundcloud have removed the necessary META data from their HTML page. After a number of years they've still not opened the API back up for public registrations. This reason alone is why we use Wistia and Omny.fm for podcasts. The code and method shown below is invalid - we'll update it if able in the future.
The Result
To demonstrate the code we'll mine the details from the NSFA National Film and Sound Archive . By default we'll render the play count, so [soundclouds url="https://soundcloud.com/nfsaaustralia/sets/early-talkback-radio-in"]
will return the value of N/A. To return the number of downloads, we'll add the type attribute as follows: [soundclouds url="https://soundcloud.com/nfsaaustralia/sets/early-talkback-radio-in" type="downloads"]
, and the shortcode will return the value of N/A.
Likes are returned as follows: type="likes"
, or type="comments"
.
The Shortcode Function
Copy and paste the WordPress function into your theme's functions.php
file or, if you sensibly have one installed, your custom functions plugin.
Use of the code requires Simple Cache and BeliefMedia Meta Tags. Both are available as WP plugins. If you're a client, both features are available by default from within the BeliefMedia plugin.
If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.
Shortcode Attributes
url
url
is the destination page for a single SoundCloud audio player.type
type
determines what data is returned; count, downloads, comments, or likes. If nothing is specified it defaults to count.n
n="0"
in your shortcode or hardcode it into the shortcode function.cache
cache
is the amount of time the data is stored locally. By default we cache data for 8 hours.PHP Function
Used outside of PHP, the following function may be used. Not unlike the WordPress version, usage requires the Simple Cache and Meta Tags functions.
Considerations
- While we mine the destination page for data, results are stored locally in your own WordPress installation using the transient API. We cache the results for 8 hours by default but, if you're comfortable doing so, you can query the SoundCloud URL more often.
- As we've done in the past for Facebook, Twitter, and other counts, you might consider altering the code to return an image rather than text. For example, using the same URL as above, we can render the play count as follows:
- Since our method of obtaining data is achieved by scraping META data, it will cease to work if SoundCloud stop providing that information.
Download
No longer available.