Created
June 15, 2016 15:44
-
-
Save kraftbj/535c3cfd2df6402f5f0024d035b68a59 to your computer and use it in GitHub Desktop.
Test Ted embed plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Ted Shortcode | |
Plugin URI: https://kraft.im/ | |
Description: Just for testing. | |
Author: Kraft | |
Author URI: https://kraft.im/ | |
Version: 2016.06.15 | |
License: GPL | |
*/ | |
wp_oembed_add_provider( '!https?://(www\.)?ted.com/talks/view/id/.+!i', 'http://www.ted.com/talks/oembed.json', true ); | |
wp_oembed_add_provider( '!https?://(www\.)?ted.com/talks/[a-zA-Z\-\_]+\.html!i', 'http://www.ted.com/talks/oembed.json', true ); | |
add_shortcode( 'ted', 'shortcode_ted' ); | |
function shortcode_ted( $atts ) { | |
global $wp_embed; | |
$defaults = array( | |
'id' => '', | |
); | |
$atts = shortcode_atts( $defaults, $atts, 'ted' ); | |
if ( empty( $atts['id'] ) ) { | |
return '<!-- Missing TED ID -->'; | |
} | |
$url = ''; | |
if ( preg_match( '#^[\d]+$#', $atts['id'], $matches ) ) { | |
$url = 'http://ted.com/talks/view/id/' . $matches[0]; | |
} elseif ( preg_match( '#^https?://(www\.)?ted\.com/talks/view/id/[0-9]+$#', $atts['id'], $matches ) ) { | |
$url = $matches[0]; | |
} | |
unset( $atts['id'] ); | |
$args = array(); | |
$args['width'] = 500; | |
$args['height'] = $args['width'] * 0.5625; | |
$args['lang'] = 'en'; | |
add_filter( 'oembed_fetch_url', 'ted_filter_oembed_fetch_url', 10, 3 ); | |
$retval = $wp_embed->shortcode( $args, $url ); | |
remove_filter( 'oembed_fetch_url', 'ted_filter_oembed_fetch_url', 10 ); | |
return $retval; | |
} | |
/** | |
* Filter the request URL to also include the $lang parameter | |
*/ | |
function ted_filter_oembed_fetch_url( $provider, $url, $args ) { | |
return add_query_arg( 'lang', $args['lang'], $provider ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment