Bandcamp 2 WP

Bandcamp support for self-hosted WordPress sites

Language: PHP

  1. <?php
  2.  
  3. // For more info:
  4. // https://giantpaper.org/blog/7453/bandcamp-snippet-for-wordpress/
  5.  
  6. add_shortcode('bandcamp', function($attr=[]){
  7. $attr = shortcode_atts([
  8. 'width' => 350,
  9. 'height' => 470,
  10. 'album' => null,
  11. 'title' => null,
  12. 'size' => 'large',
  13. 'bgcol' => 'ffffff',
  14. 'url' => null,
  15. 'linkcol' => '0687f5',
  16. 'tracklist' => 'false',
  17. ], $attr);
  18.  
  19. extract($attr);
  20.  
  21. if ($album == null)
  22. return false;
  23.  
  24. // the embed code itself
  25. $iframe = sprintf('<iframe style="border: 0; width: %s; height: %s;" src="https://bandcamp.com/EmbeddedPlayer/album=%s/size=%s/bgcol=%s/linkcol=%s/tracklist=%s/transparent=true/" seamless></iframe>',
  26. $width . 'px',
  27. $height . 'px',
  28. $album,
  29. $size,
  30. $bgcol,
  31. $linkcol,
  32. $tracklist,
  33. );
  34.  
  35. // if your site uses Gutenberg
  36. // this is veerrrry....sloppily creating your own block
  37. return '<figure class="wp-block-embed-bandcamp wp-block-embed is-type-audio is-provider-bandcamp wp-embed-aspect-16-9 wp-has-aspect-ratio js">' . '<div class="wp-block-embed__wrapper">' . $iframe . '</div>' . '</figure>';
  38. });
  39.  
  40. ?>