I have a function in my functions.php file which takes any embedded YouTube URL, appends &rel=0 to the end of the URL, and wraps it in a <div>. It works perfectly for YouTube URLs I paste into any page or post:

function embed_youtube_parameters( $code ) {
    if( strpos( $code, 'youtu.be' ) !== false || strpos( $code, 'youtube.com' ) !== false || strpos( $code, 'youtube-nocookie.com' ) !== false ) {
        $return = preg_replace( '@embed/([^"&]*)@', 'embed/$1&rel=0', $code );
    return '<div class="video-container">' . $return . '</div>';
add_filter( 'embed_oembed_html', 'embed_youtube_parameters' );

I also have a custom post type (added via a plugin I use), which has a filter that I can use to customise the output of the single custom post type pages it generates. So I have that filter also in my functions.php file.

It's quite a long filter, but part of it looks like this:

<?php if ( get_cpt_meta( 'cpt_video_link' ) ) : ?>
<div class="cpt-video">
    <?php echo wp_oembed_get( get_cpt_meta( 'cpt_video_link' ) ); ?>
<?php endif; ?>

The custom post type uses a bunch of custom fields, one of which is for a YouTube URL (cpt_video_link). So the part of the filter I've shown above should echo the YouTube URL using wp_oembed_get, which - as far as I understand it - should work the same as pasting a YouTube URL onto a regular page or post - in the sense that it's using oEmbed.

My assumption is that - because it's using oEmbed - it should therefore also work with my embed_youtube_parameters function (above), meaning that it should have &rel=0 appended to the end of the URL, and wrapped in the <div class="video-container">.

However this is not the case, and I can't figure out why. My goal is for the embed_youtube_parameters function to be applied to my CPT filter. Can anyone give me some pointers as to why this isn't happening?


After looking at the code reference for wp_oembed_get (and WP_oEmbed::get_html()) I don't think embed_oembed_html fitler gets fired when that function is called. But I might have missed something.

You could try using WP_Embed::shortcode( array $attr, string $url = '' ) instead of wp_oembed_get as this would mimic what happens with the native post types and the filter would get fired, I think.

Something like this. Updated 23.12.19

<?php if ( get_cpt_meta( 'cpt_video_link' ) ) : ?>
  <div class="cpt-video">
      $embed = new WP_Embed();
      echo $embed->shortcode(array(), get_cpt_meta( 'cpt_video_link' ));
<?php endif; ?>
| improve this answer | |
  • Thanks so much for your help! It makes sense that the embed_oembed_html filter isn't fired when wp_oembed_get is called. However, I'm afraid your suggestion doesn't work either: I now get a "wp-die-message" text displayed on the page in place of the YouTube video. The text says "There has been a critical error on your website." Any idea why? – GermanKiwi Dec 21 '19 at 0:39
  • @GermanKiwi, I'm afraid not. Have you enabled WP_DEBUG and WP_DEBUG_LOG to see what causes the error? Maybe the class is missing some data and caused the error. – Antti Koskinen Dec 21 '19 at 16:00
  • Good tip. I've just enabled both DEBUG modes and now it shows me this error on the page: Deprecated: Non-static method WP_Embed::shortcode() should not be called statically in /path/to/my/functions.php on line 45 Fatal error: Uncaught Error: Using $this when not in object context in /path/to/wp-includes/class-wp-embed.php:152 Stack trace: #0 /path/to/my/functions.php(45): WP_Embed::shortcode(Array, 'you...') #1 /path/to/wp-includes/class-wp-hook.php(288): {closure}('\t<div class="wp...', Object(WP_Post)) (etc etc) – GermanKiwi Dec 21 '19 at 17:37
  • 1
    @GermanKiwi Sorry, my bad, of course that's the reason (facepalm). So, before the echo do $embed = new WP_Embed(); to init a new instance of the class. Then echo $embed->shortcode(array(), get_cpt_meta( 'cpt_video_link' )); to use the class method. I'm not sure, if there already is an instance of the embed class in some global variable, or do you always have to initialize a new instance of it yourself every time you need it. – Antti Koskinen Dec 22 '19 at 13:44
  • Thanks so much - that worked perfectly! Perhaps you could update your answer above to include this? Then I can mark it as the answer. :) – GermanKiwi Dec 23 '19 at 1:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.