Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Test Ted embed plugin
<?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