Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

Just use the [embed] shortcode around your items.

<ul>
    <li>[embed]http://www.youtube.com/link/to/content[/embed]</li>
...
share|improve this answer
1  
I so overkilled it, didn't I... :) –  Rarst May 15 '13 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 '13 at 21:25
add comment

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 );
}
share|improve this answer
    
This works very well thanks! Is there any way I can set width of these items (but not other oEmbed items)? –  BFTrick May 15 '13 at 21:23
    
+1 for the nicest "shooting with cannons at mice" answer of this year. –  kaiser May 15 '13 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 '13 at 21:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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