Skip to content

Instantly share code, notes, and snippets.

@Viper007Bond
Created April 18, 2012 23:20
Show Gist options
  • Save Viper007Bond/2417309 to your computer and use it in GitHub Desktop.
Save Viper007Bond/2417309 to your computer and use it in GitHub Desktop.
Spotify, Rdio, and Gist embeds for WordPress
<?php
# Spotify
wp_embed_register_handler( 'spotify', '#https?://open\.spotify\.com/.*#', 'spotify_embed_handler' );
add_shortcode( 'spotify', 'spotify_shortcode' );
if ( get_option('embed_autourls') )
add_filter( 'the_content', 'spotify_embed_ids', 7 );
function spotify_embed_handler( $matches, $attr, $url, $rawattr ) {
// Let the shortcode callback do all the work.
// Don't pass through the width/height though -- it makes it too big.
return spotify_shortcode( array(), $url );
}
function spotify_shortcode( $atts, $content = '' ) {
if ( ! empty( $content ) )
$id = $content;
elseif ( ! empty( $atts['id'] ) )
$id = $atts['id'];
elseif ( ! empty( $atts[0] ) )
$id = $atts[0];
else
return '<!-- Missing Spotify ID -->';
if ( empty( $atts['width'] ) )
$atts['width'] = 300;
if ( empty( $atts['height'] ) )
$atts['height'] = 380;
$atts['width'] = (int) $atts['width'];
$atts['height'] = (int) $atts['height'];
// Spotify accepts both URLs and their Spotify ID format, so let them sort it out and validate
$embed_url = add_query_arg( 'uri', urlencode( $id ), 'https://embed.spotify.com/' );
return '<iframe src="' . esc_url( $embed_url ) . '" style="display:block; margin:0 auto; width:' . esc_attr( $atts['width'] ) . 'px; height:' . esc_attr( $atts['height'] ) . 'px;" frameborder="0" allowtransparency="true"></iframe>';
}
// Turn text like this on it's own line into an embed: spotify:track:4bz7uB4edifWKJXSDxwHcs
// The core WordPress embed functionality only works with URLs
// Modified version of WP_Embed::autoembed()
function spotify_embed_ids( $content ) {
return preg_replace_callback( '|^\s*(spotify:[^\s"]+:[^\s"]+)\s*$|im', 'spotify_embed_ids_callback', $content );
}
function spotify_embed_ids_callback( $matches ) {
return "\n" . spotify_shortcode( array(), $matches[1] ) . "\n";
}
# Rdio
wp_oembed_add_provider( '!https?://(www\.)?rdio\.com/#/.*!i', 'http://www.rdio.com/api/oembed/', true );
wp_oembed_add_provider( '!https?://rd\.io/x/.*!i', 'http://www.rdio.com/api/oembed/', true );
add_shortcode( 'rdio', 'shortcode_rdio' );
function shortcode_rdio( $atts, $content = '' ) {
global $wp_embed;
if ( empty( $atts[0] ) && empty( $content ) )
return '<!-- Missing Rdio URL -->';
$url = ( ! empty( $content ) ) ? $content : $atts[0];
// WP_Embed will take care of validation and everything else
return $wp_embed->shortcode( $atts, $url );
}
# GitHub Gist
wp_embed_register_handler( 'github-gist', '#https?://gist\.github\.com/([0-9]+)#', 'github_gist_embed_handler' );
add_shortcode( 'gist', 'github_gist_shortcode' );
function github_gist_embed_handler( $matches, $attr, $url, $rawattr ) {
// Let the shortcode callback do all the work
return github_gist_shortcode( $attr, $url );
}
function github_gist_shortcode( $atts, $content = '' ) {
if ( empty( $atts[0] ) && empty( $content ) )
return '<!-- Missing Gist ID -->';
$id = ( ! empty( $content ) ) ? $content : $atts[0];
// Parse a URL
if ( ! is_numeric( $id ) )
$id = preg_replace( '#https?://gist.github.com/([0-9]+)#', '$1', $id );
$id = (int) $id;
if ( ! $id )
return '<!-- Invalid Gist ID -->';
$embed_url = "https://gist.github.com/{$id}.js";
if ( ! empty( $atts['file'] ) )
$embed_url = add_query_arg( 'file', urlencode( $atts['file'] ), $embed_url );
return '<script src="' . esc_url( $embed_url ) . '"></script>';
}
@Viper007Bond
Copy link
Author

Rinat: Fixed, thanks. I copy/pasted this out of three different files and apparently did a crappy job at it. :)

@miya0001
Copy link

Can not embed Private Gist like below.
https://gist.github.com/77e1c2e47d7c5da0e5d1

Gist API can also specify a file name like below.
https://gist.github.com/1967049#file_example.php

Please see my plugin source. :)
http://wordpress.org/extend/plugins/oembed-gist/

Thanks!

@Viper007Bond
Copy link
Author

Gist doesn't allow the embedding of private Gists and you can embed a specific file using the "file" parameter: [gist https://gist.github.com/1967049 file="file_example.php" /] or [gist file="file_example.php"]https://gist.github.com/1967049[/gist]

@miya0001
Copy link

Oh, OK.
Thanks. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment