Created
April 23, 2014 16:28
-
-
Save roborourke/11222368 to your computer and use it in GitHub Desktop.
A WordPress plugin for embedding content from vine
This file contains hidden or 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: Vine oEmbed | |
Plugin URI: http://interconnectit.com | |
Description: Registers an oEmbed handler for Vine URLs | |
Version: 1.0 | |
Author: Robert O'Rourke | |
Author URI: http://interconnectit.com | |
*/ | |
if ( ! class_exists( 'vine_oembed' ) ) { | |
class vine_oembed { | |
// used to match embeddable URL | |
public $regex = '#https?://vine.co/v/([^/]*)(/embed/(simple|postcard)(\?audio=1)?)?#i'; | |
public function __construct() { | |
add_action( 'init', array( $this, 'init' ) ); | |
// modifies the js tag to have async and charset attributes as per vine embed code | |
add_filter( 'script_loader_src', array( $this, 'async_script_tag' ), 10, 2 ); | |
// remove the <p> tags from iframe | |
add_filter( 'the_content', array( $this, 'unpee' ), 20 ); | |
} | |
public function init() { | |
wp_embed_register_handler( 'vine', $this->regex, array( $this, 'handler' ) ); | |
} | |
// returns the iframe | |
public function handler( $matches, $attr, $url, $rawattr ) { | |
// default type | |
$type = '/embed/simple'; | |
if ( isset( $matches[2] ) ) | |
$type = $matches[2]; | |
$embed = sprintf( | |
'<iframe class="vine-embed" src="https://vine.co/v/%1$s%2$s" width="600" height="600" frameborder="0"></iframe>', | |
esc_attr( $matches[1] ), | |
$type | |
); | |
// vine async embed js | |
if ( ! is_admin() ) | |
wp_enqueue_script( 'vine-embed', '//platform.vine.co/static/scripts/embed.js' ); | |
return apply_filters( 'embed_vine', $embed, $matches, $attr, $url, $rawattr ); | |
} | |
// adds filter to esc_url if we're rendering vine embed script | |
public function async_script_tag( $src, $handle ) { | |
if ( $handle === 'vine-embed' ) | |
add_filter( 'clean_url', array( $this, 'mod_script_tag' ), 11, 1 ); | |
return $src; | |
} | |
// adds attributes to vine embed script tag | |
public function mod_script_tag( $src ) { | |
remove_filter( 'clean_url', array( $this, 'mod_script_tag' ), 11 ); | |
return "{$src}' async charset='utf-8"; | |
} | |
// removes p tags from iframes | |
public function unpee( $content ) { | |
$content = preg_replace( '/<p>(<a [^>]*?><iframe [^>]*?><\\/iframe><\\/a>|<iframe [^>]*?><\\/iframe>){1}<\\/p>/s', '<div class="embed-iframe">$1</div>', $content ); | |
return $content; | |
} | |
} | |
$vine_oembed = new vine_oembed(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@sanchothefat good snippet, which can be usefull for add wp 4.0 fallback support.
But I think it will be better to use
entry-content-asset
instead ofembed-iframe
class, because Wordpress by default wraps all oembeded iframes inentry-content-asset
class..