Skip to content

Instantly share code, notes, and snippets.

@roborourke
Created April 23, 2014 16:28
Show Gist options
  • Save roborourke/11222368 to your computer and use it in GitHub Desktop.
Save roborourke/11222368 to your computer and use it in GitHub Desktop.
A WordPress plugin for embedding content from vine
<?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();
}
@vburlak
Copy link

vburlak commented Apr 6, 2015

@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 of embed-iframe class, because Wordpress by default wraps all oembeded iframes in entry-content-asset class..

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