When I try to embed a YouTube video using one of the following ways:

  • By entering a YouTube URL
  • By entering an shortcode
  • By adding a video through the media browser (which results in an shortcode)

I am seeing the video just fine in the editor view:

enter image description here

But in the frontend view, using this code:

 $content_desktop = do_shortcode(get_the_content());

I am seeing

  • when entering a pure URL: the unparsed URL enter image description here

  • When entering an shortcode or using the WYSIWYG editor: literally nothing enter image description here

Things I’ve checked:

  • I’m the admin user so there should be no problems with rights
  • Other shortcodes work fine
  • The DOM literally shows what I show above, there is no CSS interference
  • The YouTube videos I’m trying to embed allow external embedding
  • There are no options for me to check in “Settings” > “Media” (apparently it used to be that you had to explicitly turn on media embedding there, but no more)

Is there something well known that could be causing this?

1 Answer
1

I’ve just looked at the source of the WP_Embed class, and it appears they are not actually registering a shortcode, but hooking into the the_content filter.

Change your code to

$content_desktop = apply_filters("the_content", get_the_content());

or manually trigger their filter with something like

$content_desktop = WP_Embed::run_shortcode(get_the_content());

or, if you prefer to have an object:

$myembeds = new WP_Embed;
$content_desktop = $myembeds->run_shortcode(get_the_content());

See also WP_Embed::run_shortcode in the codex, and the source code of class-wp-embed.php.

Leave a Reply

Your email address will not be published. Required fields are marked *