I am trying to find a function that uses mediaelement.js player to embed relatively brandless youtube videos.
As mediaelement.js is built into WordPress surely I can replace the youtube branded iframe embed code given back from oembed with a chromeless mediaelement api embed code.
I have 2000 videos on my site and going into every video to change the youtube embed is sadly not an option. This must be possible with a function!
In my tests the mediaelement api seems to suck on ipad so I have used an if statement to exclude ios devices. Using a little Jquery this code works well in a wordpress php file.
<?php
if(strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'],'iPod') || strstr($_SERVER['HTTP_USER_AGENT'],'iPad'))
{
echo '<iframe width="560" height="315" src="//www.youtube.com/embed/TKgDzpc3lZ8?rel=0&showinfo=0" frameborder="0" allowfullscreen></iframe>';
}
else
{
echo '<video controls="controls" id="youtube1">
<source type="video/youtube" src="//www.youtube.com/embed/TKgDzpc3lZ8"/>
</video>';
}
?>
So with that working as it should the goal now is do this with a function and str_replace the youtube embed before it is sent to the browser. This is what I have so far and I am well aware I am punching above my weight:
function youtubeme($youtubeme) {
if(strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'],'iPod') || strstr($_SERVER['HTTP_USER_AGENT'],'iPad'))
{
}
else
{
return (str_replace('<iframe width="750" height="422"', '<video controls="controls" id="youtube1"><source type="video/youtube" ', $youtubeme));
return (str_replace('?feature=oembed&autoplay=1" frameborder="0" allowfullscreen=""></iframe>', '"/></video>', $youtubeme));
}}
add_action('wp_embed', 'youtubeme');
Any help would be very much appreciated as I am still learning and it has been several hours of pain getting this far.
//////EDIT//////
This is the Jquery I used.
jQuery(document).ready(function($) {
$('#youtube1').mediaelementplayer({
alwaysShowControls: true,
// force iPad's native controls
iPadUseNativeControls: true,
// force iPhone's native controls
iPhoneUseNativeControls: true,
// force Android's native controls
AndroidUseNativeControls: true,
})});
You were very near, basically just missing the correct filter hook. Note that:
the
$html
parameter is the<iframe...
HTML.$url
contains the URL being parsed (youtu.be/...
oryoutube.com/...
).$attr
is an object withheight
andwidth
.the
$html
string replace should be optimized to work with anywidth/height
; in this example I’m using the values generated by one theme, but it varies from one to another.the
<video id="ID">
should be optimized to work with any number of embeds.the
play
icon is showing two versions: YouTube’s and MediaElement’sI didn’t manage to instantiate MediaElement with
$('#youtube1').mediaelementplayer()
and usednew MediaElementPlayer("#youtube1")
.And the following to enqueue the needed scripts/stylesheet: