1

I'm aware that WordPress has oEmbed functionality. But the content to be embedded has to be on it's own line like so:

Lorem Ipsum

http://www.youtube.com/link/to/content

More Lorem Ipsum

Unfortunately, my markup looks like this so oEmbed doesn't work and I have to use a plugin.

<ul>
    <li>http://www.youtube.com/link/to/content</li>
    <li>http://www.youtube.com/link/to/content</li>
</ul>

Is there anyway I can get oEmbed to work without this plugin? A line or two in functions.php would be better than this plugin.

0

2 Answers 2

3

Just use the [embed] shortcode around your items.

<ul>
    <li>[embed]http://www.youtube.com/link/to/content[/embed]</li>
...
2
  • 1
    I so overkilled it, didn't I... :)
    – Rarst
    May 15, 2013 at 20:29
  • This works great because it's already built in (I can't believe I missed it!). The only criticism is that I have to teach users to type in the embed code with a specific width. Hmmm.
    – BFTrick
    May 15, 2013 at 21:25
2

There is bit of special filtering by WP_Embed class that turns standalone links into target for embedding:

/**
 * Passes any unlinked URLs that are on their own line to {@link WP_Embed::shortcode()} for potential embedding.
 *
 * @uses WP_Embed::autoembed_callback()
 *
 * @param string $content The content to be searched.
 * @return string Potentially modified $content.
 */
function autoembed( $content ) {
    return preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', array( $this, 'autoembed_callback' ), $content );
}

If we take that logic and adjust regex for your use case:

add_filter( 'the_content', 'autoembed_list_items', 8 );

function autoembed_list_items( $content ) {

    global $wp_embed;

    return preg_replace_callback( '|<li>(https?://[^\s"]+)</li>|im', array( $wp_embed, 'autoembed_callback' ), $content );
}
3
  • This works very well thanks! Is there any way I can set width of these items (but not other oEmbed items)?
    – BFTrick
    May 15, 2013 at 21:23
  • +1 for the nicest "shooting with cannons at mice" answer of this year.
    – kaiser
    May 15, 2013 at 21:39
  • @BFTrick might be possible, but will need to reverse-engineer much more of it and I already overkilled it in first place :)
    – Rarst
    May 15, 2013 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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