With newer versions of WordPress, YouTube auto oEmbeds. On this particular site I am working on, I need a unique div
wrapper around the embedded iframe
. I had a look in my functions.php
but couldn’t see anything related to YouTube. Where would I find the code I need to add my extra html?
2 Answers
Personally, I found the oembed_dataparse
filter pretty fiddly to use; sometimes it worked, sometimes it didn’t; and when used in conjunction with custom TinyMCE instances, it seems as though the added wrapper was hard-baked into the content rather than added via the filter at output.
I found the embed_oembed_html
filter much more reliable and works every time:
function vnmFunctionality_embedWrapper($html, $url, $attr, $post_id) {
return '<div class="embedwrapper">' . $html . '</div>';
}
add_filter('embed_oembed_html', 'vnmFunctionality_embedWrapper', 10, 4);
Note that this will wrap all oEmbeds. If you wanted to target YouTube specifically:
function vnmFunctionality_embedWrapper($html, $url, $attr, $post_id) {
if (strpos($html, 'youtube') !== false) {
return '<div class="youtubewrapper">' . $html . '</div>';
}
return $html;
}