Skip Navigation

[Resolved] Youtube embeds not working

This support ticket is created 5 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by mikeH-3 5 years, 5 months ago.

Assisted by: Luo Yang.

Author
Posts
#1329621

I am using a media field to allow people to paste in youtube urls like: hidden link

Then I use [types field='sermon-video-url'][/types] to show the videos, which it does nicely on the frontend of website. But, ever since I added this line into my functions.php file:

add_filter('embed_oembed_html', 'soundCloud_mini_embed', 10, 3);  

It stops youtube embeds from showing anymore. Now nothing shows where the video should be showing. I edited the embed for soundcloud, so that's why that line even exists in my functions.php file. I need to keep it in there. So how do I get the youtube video to show too? I don't see how one should affect other.

Thanks!

#1329765

Hello,

The PHP codes you mentioned above, will add a filter into filter hook "embed_oembed_html" with PHP function "soundCloud_mini_embed", see WordPress document:
https://developer.wordpress.org/reference/functions/add_filter/#parameters
$function_to_add
(callable) (Required) The callback to be run when the filter is applied.

there might some problem in that PHP function "soundCloud_mini_embed", how do you setup the PHP function "soundCloud_mini_embed"?
With other plugin/theme? please provide detail steps to duplicate the same problem, thanks

#1329769

The whole function is this. Does that help? As soon as I remove the add_filter('embed_oembed_html', 'soundCloud_mini_embed', 10, 3); line, youtube embeds work again.

function soundCloud_mini_embed($html, $url) {
  
  // Only use this filter on Soundcloud embeds
  if(preg_match("/soundcloud.com/", $url)) {
    
    // array of patterns to find in the oembed iframe html
    $patterns = array();
      $patterns[0] = "/visual=true/"; // true means a big image background
      $patterns[1] = "/show_artwork=true/"; // true means show the track artwork
      $patterns[2] = "/ height=\"\d+?\"/"; // height of standard embed is in the 400-pixel range. Just look for any height integer
      $patterns[3] = "/ width=\"\d+?\"/"; // width of standard embed in WordPress seems to be a fixed pixel width. Look for any integer
    
    // array of replacements to make for these patterns
    $replacements = array();
      $replacements[0] = "visual=false"; // turn off big image background
      $replacements[1] = "show_artwork=false"; // turn off track artwork
      $replacements[2] = " height=\"166\""; // set iframe height to 166 pixels, the embed standard for the Soundcloud mini player
      $replacements[3] = " width=\"100%\""; // set iframe to full width instead of fixed pixel dimension
    
    // prophylactic ksort to make sure that all patterns and replacments will line up regardless of what order they're input
    ksort($patterns);
    ksort($replacements);
    
    // one function to do all the find and replace
    $html = preg_replace($patterns, $replacements, $html);
    // return the html string and save to database or output
    return $html;
  }
  
}
// hook into the WordPress oembed filter
add_filter('embed_oembed_html', 'soundCloud_mini_embed', 10, 3);

Basically I have to do all this because I want the smaller mini soundcloud template and not the square one oembed does by default.

#1329777

There is a bug in the PHP codes you mentioned above, please try to change these lines from:

// return the html string and save to database or output
    return $html;
  }

To:

// return the html string and save to database or output
  }
return $html;

And test again

#1330429

Thanks! Worked perfectly!