“I have an idea for a plugin!” he said to me. Everyone does, but I encouraged this question. “I want to make the media embeds in WordPress more responsive.” I paused. There are already a lot of those, like Responsive Video Shortcodes. So I asked what made his different. “I’m going to make NEW shortcodes!”
I see a lot of plugins. Many times I see plugins where people want to tweak a normal part of WordPress, like the gallery or the embeds. The problem I see is that instead of extending those, they decide it’s better to make a plugin that recreates all the shortcodes and embeds, but with a new name, so users have to remember to use (which is default to WordPres) or
[youtube]
(which is in Jetpack) or [someothervideotool]
(which I made up for an example).
Now I’m guilty of this myself! I have a private plugin which is HTML5 video embeds and it gives the video in HTML5 format, which I could do in the default embed code, but it also plunks a link underneath that says “Can’t see the video? Click here.” And the Click Here goes right to the mp4 video. Because I have some rather dim people. But. I don’t have to reinvent the wheel here. I could instead filter that shortcode just to put my little blurb on the end! Doesn’t that seem smarter?
The first time I thought about this, I said “What I need is to filter and slap something on the end!”
function halfelf_oembed_filter($html, $url, $attr) { $html .= "<p>I am a monkey.</p>"; return $html; } add_filter( 'embed_oembed_html', 'halfelf_oembed_filter', 10, 3 ); add_filter( 'video_embed_html', 'halfelf_oembed_filter', 10, 3 );
This puts “I am a monkey” below every embed, which is great for stuff like YouTube and Twitter. The second filter is for videos when you’re using Jetpack. And this works great, I could even wrap the HTML in something, like a div or js code, and enforce responsiveness. You can check the URLs, see if it’s youtube or whatever, and apply different settings per URL. Works nicely. I actually have my just div-wrapping for padding reasons.
$html = "<div style='padding: 5px;'>".$html."</div>";
This works brilliantly, however … I’ll get to that in a second. Now, for my friend’s initial idea, once we banged on this, he said he was just going to use the other plugin, but embed-filtering was way easier than trying to over-ride the shortcodes and reinvent the wheel. It also makes it easier to upgrade.
But then there’s my problem with my site and the need for that “Hey, get the MP4 here!” message. You see, 90% of the videos I have on this particular site are locally uploaded, and since I always upload an mp4 and a webm (and sometimes an ogg), how do I parse it?
First of all, I’m using the Video Shortcode, so I know there’s a simple filter for wp_video_shortcode_override
(it works roughly the same as img_caption_shortcode
but that’s not what I want to use. I don’t want to replace everything, that’s my whole point here. I just want to make a link! In addition, I know I’m only going to want this when I have a video with a defined mp4, so why not just filter wp_video_shortcode
instead?
So that’s what I did:
function halfelf_video_shortcode($html, $attr) { if ( !empty( $attr['mp4'] ) ) { $html .= "<p>Can't see the whole video? Click <a href='".$attr['mp4']."'>here</a>.</p>"; } return $html; } add_filter( 'wp_video_shortcode', 'halfelf_video_shortcode', 10, 2);
This slaps a link for my tech challenged friends on browsers that refuse to tolerate videos.