Problems, need help? Have a tip or advice? Post it here.
3 posts Page 1 of 1
Hi all,

I've been searching around this forum and around the internets, but i haven't found an answer.

I'm trying to write a shortcode in kfunctions.php for embedding bandcamp.com content. I've manage to create one for vimeo too and it works like a charm, but somehow i cant get a shortcode for bandcamp working.

Here's what i have so far (youtube and vimeo)
Code: Select all
<?php
   // YouTube Shortcode
   // Usage:   [youtube video="http://www.youtube.com/watch?v=5PsnxDQvQpw"]
   //          [youtube http://www.youtube.com/watch?v=1aBSPn2P9bg]
   //          [youtube 1aBSPn2P9bg]
   $FUNCS->register_shortcode( 'youtube', 'youtube_handler' );
   function youtube_handler( $params, $content=null ){
      global $FUNCS;
     
      extract( $FUNCS->get_named_vars(array(
         'video' => 'http://',
         'width' => '418',
         'height' => '215',
      ), $params) );

      // Video parameter is link or ID?
      if ( (substr($video, 0, 7) == 'http://') || (substr($video, 0, 8) == 'https://') ){
         /*
         Example links that can be handled:
         http://www.youtube.com/watch?v=5PsnxDQvQpw
         http://youtube.com/watch?v=5PsnxDQvQpw
         http://youtube.com/watch?gl=US&hl=en-US&v=5PsnxDQvQpw
         http://youtube.com/v/5PsnxDQvQpw&rel=1
         */
         if( !preg_match('#https?://(?:[^\.]+\.)?youtube.com.*(?:\?v=|&v=|/v/)([\w_-]+)#i', $video, $matches) ) return;
         $video = $matches[1];
      }

      // Sanitize parameters
      $video = htmlspecialchars( $video, ENT_QUOTES );
      $width = (int)$width;
      $height = (int)$height;
     
      // Output HTML
      $html =<<<EOS
      <iframe class="youtube" type="text/html" width="$width" height="$height" src="http://www.youtube.com/embed/$video?rel=0&autohide=1" frameborder="0"></iframe>
EOS;
      return $html;
   }

   // Vimeo Shortcode
   // Usage: [vimeo 40613192]
   $FUNCS->register_shortcode( 'vimeo', 'vimeo_handler' );
   function vimeo_handler( $params, $content=null ){
      global $FUNCS;
     
      extract( $FUNCS->get_named_vars(array(
         'id'=>'',
         'width' => '418',
         'height' => '215',
      ), $params) );

      // Sanitize parameters
      $id = htmlspecialchars( $id, ENT_QUOTES );
      $width = (int)$width;
      $height = (int)$height;
     
      // Output HTML
      $html =<<<EOS
      <iframe class="youtube" src="http://player.vimeo.com/video/$id?byline=0&amp;portrait=0&amp;color=FFFFFF;badge=0" width="$width" height="$height" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
EOS;
      return $html;
   }


?>


The embed-code for bandcamp is
Code: Select all
<iframe style="border: 0; width: 100%; height: 120px;" src="http://bandcamp.com/EmbeddedPlayer/album=**********/size=medium/bgcol=ffffff/linkcol=0687f5/t=*/transparent=true/" seamless></iframe>

Where ********** is the album ID and * is the track ID.

Any help would be appreciated :)
Hello and welcome Azuil :)

Following is the required shorcode that should work with the latest version of Bandcamp embed codes (http://blog.bandcamp.com/2013/06/12/all ... itter-too/).
Please copy/paste it into your kfunctions.php.
Code: Select all
    // Bandcamp (http://bandcamp.com/)
    // Usage:
    // Preferred method is to use the 'wordpress.com' embed code made available
    // by Bandcamp for each album/track as explained at http://blog.bandcamp.com/2013/06/12/all-new-players-up-twitter-too/
    //
    // The syntax of embed code obtained (as explained above) goes something like the following:
    // LARGE size
    // [bandcamp width=360 height=360 album=1949149167 size=large bgcol=ffffff linkcol=0687f5]
    // [bandcamp width=360 height=480 album=1949149167 size=large bgcol=ffffff linkcol=0687f5 notracklist=true]
    //
    // MEDIUM size (i.e. horizontal)
    // [bandcamp width=100% height=120 album=1949149167 size=medium bgcol=ffffff linkcol=0687f5]
    // [bandcamp width=400 height=120 album=1949149167 size=medium bgcol=ffffff linkcol=0687f5]
    //
    // SMALL size
    // [bandcamp width=100% height=42 album=1949149167 size=small bgcol=ffffff linkcol=0687f5]
    // [bandcamp width=100% height=42 album=1949149167 size=small bgcol=ffffff linkcol=0687f5 artwork=false]
    // [bandcamp width=350 height=42 album=1949149167 size=small bgcol=ffffff linkcol=0687f5 artwork=false]
    // [bandcamp width=100% height=42 album=1949149167 size=small bgcol=ffffff linkcol=0687f5 artwork=false t=3]
   
    $FUNCS->register_shortcode( 'bandcamp', 'bandcamp_handler' );
    function bandcamp_handler( $params, $content=null ){
        global $FUNCS, $CTX;
       
        extract( $FUNCS->get_named_vars(array(
            'album'         => '',          // album id
            'size'         => 'large',      // large, medium, small
            'bgcol'         => 'ffffff',   // hex without prefixing '#'
            'linkcol'      => '0687f5',   // - do -
            'notracklist'   => '',          // true
            'artwork'       => '',          // false
            'width'         => '',      
            'height'      => '',      
            't'             => '',          // track id
        ), $params) );
       
        $url = "http://bandcamp.com/EmbeddedPlayer/v=2/";
        if( $album ){
            $url .= "album={$album}";
        } else {
            return "[bandcamp: shortcode must include an album id]";
        }
       
        $url .= "/size={$size}";
        $url .= "/bgcol={$bgcol}";
        $url .= "/linkcol={$linkcol}";
        if( $notracklist == "true" ){
            $url .= "/notracklist=true";
        }
        if( $artwork == "false" ){
            $url .= "/artwork=false";
        }
        if( $t ){
            $url .= "/t={$t}";
        }
        $url .= '/';
       
        return "<iframe width='" . $width . "' height='" . $height . "' src='" . $url . "' allowtransparency='true' frameborder='0'></iframe>";
    }

As noted in the code's comment above, you can get the embed code from bandcamp - choose the 'wordpress.com' variety.

Hope this helps.

Thanks.
This works like a charm, thank you very much!
3 posts Page 1 of 1
cron