Adding a wrapper to the youtube embed automatically?

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;
}

error code: 523