Created
July 21, 2015 17:58
-
-
Save cgrymala/652c8f9e4bd8b211eba8 to your computer and use it in GitHub Desktop.
Attempt to make JetPack Shortcode Embeds compatible with Fluid Video Embeds 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 | |
/** | |
* Attempts to override the stupid way JetPack short-circuits native oEmbeds | |
* so that the Fluid Video Embeds plugin can work properly | |
*/ | |
function fve_jetpack_filter_video_embed( $html, $atts=array() ) { | |
/** | |
* Attempt to make sure the Fluid Video Embeds plugin is | |
* active and that the global $fve object is instantiated | |
*/ | |
if ( ! class_exists( 'Fluid_Video_Embeds' ) ) | |
return $html; | |
global $fve; | |
if ( ! isset( $fve ) ) { | |
Fluid_Video_Embeds::instance(); | |
} | |
if ( ! isset( $fve ) ) { | |
return $html; | |
} | |
/** | |
* If this isn't a YouTube or Vimeo video, bail out | |
*/ | |
if ( ! stristr( $html, 'youtube' ) && ! stristr( $html, 'vimeo' ) ) | |
return $html; | |
/** | |
* If the src key exists in the $atts array, this was called from | |
* the wp_video_shortcode filter, which means we can just send | |
* the video URL instead of trying to parse the HTML output | |
*/ | |
if ( is_array( $atts ) && array_key_exists( 'src', $atts ) ) { | |
return $fve->filter_video_embed( '', $atts['src'] ); | |
} | |
/** | |
* If the atts array didn't contain the video URL/src, we need to try to parse the | |
* embed HTML to see if we can find the appropriate information | |
* This is a basic example of trying to find a YouTube URL. We don't have an | |
* equivalent method of finding a Vimeo URL, yet, so that would have to be | |
* written separately. | |
*/ | |
preg_match( '`http(s*?):\/\/(www\.*?)youtube.com\/embed\/([a-zA-Z0-9]{1,})`', $html, $matches ); | |
if ( ! empty( $matches[3] ) ) | |
return $fve->filter_video_embed( $html, sprintf( 'https://youtube.com/watch?v=%s', $matches[3] ), null ); | |
return $html; | |
} | |
/** | |
* wp_video_shortcode() is a native WordPress function | |
*/ | |
add_filter( 'wp_video_shortcode', 'fve_jetpack_filter_video_embed', 16, 2 ); | |
/** | |
* video_embed_html is a filter contained within JetPack | |
*/ | |
add_filter( 'video_embed_html', 'fve_jetpack_filter_video_embed', 16 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
check this one too : jamiehs/fluid-video-embeds#2