RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts</strong | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.69% (5.89%*) • Home Loan Fixed: 5.39% (6.59%*) • Fixed: 5.39% (6.59%*) • Variable: 5.69% (5.89%*) • Investment IO: 5.69% (6.48%*) • Investment PI: 5.39% (6.59%*)

Insert Live SoundCloud Play Counts, Download Counts, Comment Counts, and Like Counts in WordPress with Shortcode (without the API)

Insert Live SoundCloud Play Counts, Download Counts, Comment Counts, and Like Counts in WordPress with Shortcode (without the API)

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.

1
<?php 
2
/*
3
 Insert SoundCloud Data in WordPress with Shortcode (without the API)
4
 http://www.beliefmedia.com/soundcloud-data-wordpress
5
*/
6
 
7
function beliefmedia_soundcloud_data($atts) {
8
 
9
  $atts = shortcode_atts(array(
10
    'url' => '', /* SoundCloud Clip URL */
11
    'type' => 'count', /* count, downloads, comments, likes */
12
    'n' => 1,
13
    'cache' => 3600 * 8
14
  ), $atts);
15
 
16
  if ($atts['url'] == '') return 'SoundCloud URL is required.';
17
 
18
  if ($type == '') $type = 'count';
19
  $cache = $atts['cache'];
20
  $transient = 'scdata_' . md5(serialize($atts));
21
 
22
  $cached = get_transient($transient);
23
  if ($cached !== false) {
24
    return $cached;
25
 
26
   } else {
27
 
28
   switch ($atts['type']) {
29
     case 'count':
30
       $type = 'soundcloud:play_count';
31
       break;
32
     case 'downloads':
33
       $type = 'soundcloud:download_count';
34
       break;
35
     case 'comments':
36
      $type = 'soundcloud:comments_count';
37
       break;
38
     case 'likes':
39
       $type = 'soundcloud:like_count';
40
       break;
41
     default:
42
       $type = 'soundcloud:play_count';
43
       break;
44
   }
45
 
46
   /* Get all meta data from page URL */
47
   $args = array('cache' => $cache);
48
   $return = beliefmedia_meta_data($atts['url'], $type, $args);
49
 
50
   /* Number format? */
51
   if ($atts['n']) $return = number_format($return);
52
 
53
   /* Save transient */
54
   set_transient($transient, $return, $atts['cache']);
55
 
56
  return $return;
57
 }
58
}
59
add_shortcode('soundclouds', 'beliefmedia_soundcloud_data');

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

The url is the destination page for a single SoundCloud audio player.

type

The type determines what data is returned; count, downloads, comments, or likes. If nothing is specified it defaults to count.

n

By default the value returned will be in a number format with grouped thousands. To turn this feature off, use n="0" in your shortcode or hardcode it into the shortcode function.

cache

The 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.

1
<?php 
2
include('../simple-cache/cache.inc.php');
3
include('../meta/meta.php');
4
 
5
/*
6
 Insert SoundCloud Data in WordPress with Shortcode (without the API)
7
 http://www.beliefmedia.com/soundcloud-data-wordpress
8
*/
9
 
10
function beliefmedia_soundcloud_data($url, $type = 'count', $cache = 28800) {
11
 
12
  $n = true;
13
 
14
  if ($url == '') return 'SoundCloud URL is required.';
15
 
16
  if ($type == '') $type = 'count';
17
  $transient = 'scdata_' . md5(serialize(func_get_args()));
18
 
19
  $cached = beliefmedia_get_transient($transient, $cache);
20
  if ($cached !== false) {
21
    return $cached;
22
 
23
   } else {
24
 
25
   switch ($type) {
26
     case 'count':
27
       $type = 'soundcloud:play_count';
28
       break;
29
     case 'downloads':
30
       $type = 'soundcloud:download_count';
31
       break;
32
     case 'comments':
33
      $type = 'soundcloud:comments_count';
34
       break;
35
     case 'likes':
36
       $type = 'soundcloud:like_count';
37
       break;
38
     default:
39
       $type = 'soundcloud:play_count';
40
       break;
41
   }
42
 
43
   /* Get all meta data from page URL */
44
   $args = array('cache' => $cache);
45
   $return = beliefmedia_meta_data($url, $type, $args);
46
 
47
   /* Number format? */
48
   if ($n !== false) $return = number_format($return);
49
 
50
   /* Save transient */
51
   beliefmedia_set_transient($transient, $return);
52
 
53
  return $return;
54
 }
55
}
56
 
57
/* Usage */
58
echo beliefmedia_soundcloud_data($url = 'https://soundcloud.com/nfsaaustralia/sets/early-talkback-radio-in');
59
 
60
/* Usage (download count) */
61
echo beliefmedia_soundcloud_data($url = 'https://soundcloud.com/nfsaaustralia/sets/early-talkback-radio-in', $type = 'downloads');

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:

N/A

  • 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.

■ ■ ■

 
Download our complimentary 650-page guide on marketing for mortgage brokers. We'll show you exactly how we generate billions in volume for our clients.
Finance Guide, Cropped Top and Bottom
  Timezone: 1 · [ CHANGE ]

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment