-
-
Save ideag/4a69a2035ed1e02946a6 to your computer and use it in GitHub Desktop.
oEmbed JSFiddle links into WordPress posts
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: JSFiddle oEmbed | |
Plugin URI: http://klausk.aruno.lt/jsfiddle/ | |
Description: Embed JSFiddle into posts | |
Version: 1.0 | |
Author: IdeaG | |
Author URI: http://klausk.aruno.lt | |
License: GPL2 | |
Based on Gist-oembed by ninnypants - https://gist.github.com/ninnypants/5054541 | |
*/ | |
class JSFiddle_oEmbed { | |
public function __construct(){ | |
add_action( 'init', array( $this, 'setup_handler' ) ); | |
} | |
public function result( $url ){ | |
global $content_width; | |
$url = $url[0]; | |
$height = '300'; | |
$width = $content_width?$content_width:'100%'; | |
$query = parse_url($url,PHP_URL_QUERY); | |
$url = trailingslashit(preg_replace('#\?.+#i', '', $url)); | |
parse_str($query,$query); | |
if (isset($query['height'])) { | |
$height = $query['height']; | |
} | |
if (isset($query['width'])) { | |
$width = $query['width']; | |
} | |
if( !preg_match( '#/embedded/#i', $url ) ) | |
$url .= 'embedded/'; | |
return '<iframe width="'.$width.'" height="'.$height.'" src="'.$url.'" allowfullscreen="allowfullscreen" frameborder="0"></iframe>'; | |
} | |
public function setup_handler(){ | |
wp_embed_register_handler( 'jsfiddle', '#https?://jsfiddle.net/.*#i', array( $this, 'result' ) ); | |
} | |
} | |
$jsfiddle_oembed = new JSFiddle_oEmbed(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment