WordPress supports quite a few embeds including YouTube since WordPress 2.9.
To render anywhere, just pass your content through the_content
filter and it should automatically convert the urls.
$oembedContent = apply_filters( 'the_content', $basicContent );
If the defaults don’t meet your needs you can add an oEmbed-Enabled site using wp_oembed_add_provider( $format, $provider, $regex )
or add non oEmbed-Enabled sites using wp_embed_register_handler( $id, $regex, $callback, $priority )
. GenerateWP includes a section to generate add_provider code.
To test them out, run your url through wp_oembed_get()
$embed_code = wp_oembed_get( $url, $args );
and filter the returned oEmbed HTML with oembed_dataparse
. There is a good example of using oembed_dataparse
at lorut to make some video embeds responsive.
// Hook onto 'oembed_dataparse' and get 2 parameters
add_filter( 'oembed_dataparse', 'responsive_wrap_oembed_dataparse', 10, 2 );
function responsive_wrap_oembed_dataparse( $html, $data ) {
// Verify oembed data (as done in the oEmbed data2html code)
if ( ! is_object( $data ) || empty( $data->type ) ) {
return $html;
}
// Verify that it is a video
if ( ! ( $data->type == 'video' ) ) {
return $html;
}
// Calculate aspect ratio
$ar = $data->width / $data->height;
// Set the aspect ratio modifier
$ar_mod = ( abs( $ar - ( 4 / 3 ) ) < abs( $ar - ( 16 / 9 ) ) ? 'embed-responsive-4by3' : 'embed-responsive-16by9' );
// Strip width and height from html
$html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
// Return code
return '<div class="embed-responsive ' . $ar_mod . '" data-aspectratio="' . number_format( $ar, 5, '.' ) . '">' . $html . '</div>';
}
WordPress 4.4 introduced a oEmbed discovery to allow WP links to be embedded. amaze.website wrote an article on how to style your embeds using embed_head
and embed_footer
.
// Add custom footer after embed links preview
add_action( 'embed_footer', 'embed_custom_footer_line' );
function embed_custom_footer_line(){ ?>
<div id="custom-embed-footer">
<h3>Our custom Footer line!</h3>
</div>
<?php }