I am using the content-video.php include to show the latest video post on my homepage. The include only shows the video from the post and not the rest of the content, which works well.

My problem is, if I add any content (eg - text, another link, etc) after the link to the video in the post, it breaks my video embed on the homepage. The video and text before and after display fine in the actual post.

Here is the code I have for content-video.php

<?php if(strlen( get_the_title() ) >0 ): ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php endif; ?>                       
    $parsedUrl  = parse_url(get_the_content());
    $embed      = $parsedUrl['query'];
    parse_str($embed, $out);
    $embedUrl   = $out['v'];
<iframe width="385" height="217" src="http://www.youtube.com/embed/<?php echo $embedUrl; ?>" frameborder="0" allowfullscreen></iframe>

And, here is the loop code where I'm including it on the homepage:

    <!--latest video--> 
        $args = array(
            'post_type' => 'post', // if the post type is post 
            'posts_per_page' => 1,
            'tax_query' => array(
                'taxonomy' => 'post_format',
                'field' => 'slug',
                'terms' => 'post-format-video'
        $my_query = new WP_Query( $args );
        <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <div id="latestVideo">
            <?php get_template_part( 'content', 'video' ); ?>
            <a class="more-videos" href="/type/video/">more videos</a>
    <?php endwhile; ?>

Can anyone spot what I'm doing wrong, or do I need to embed the video in a different way?!

Thanks for any help.

  • parse_url(get_the_content()) expects a plain URL. Anything else will break it.
    – fuxia
    May 28, 2013 at 1:56
  • Thanks very much for your quick reply, Toscho. Is there any way around that? I just want to find the first video link in the post and not display the rest of the content. I haven't been able to find any other examples.
    – Desmond
    May 28, 2013 at 2:19

2 Answers 2


If you don’t know what the content is, use a regular expression to test if there is an URL.

Sample code:

$content = get_the_content();
$pattern = '~([^"\'])?(https?://.*\.(flv|aac|mp4|mov|m4a|f4a|ogg|oga|mp3))([^"\'\?])?~i';

if ( preg_match( $pattern, $content, $matches ) )
    $parsedUrl  = parse_url( $matches[ 0 ] );
    // create the vidoe markup
    // echo $content or something else
  • Thanks for your reply Toscho, and taking the time to write the code. I've been trying to implement it over the last few days, but I can't get it to work - it keeps defaulting to the "else" option, even though there is a YouTube URL in the post. I tried different regular expressions to get the YouTube URL, but no luck. Do you have any ideas? Thanks again.
    – Desmond
    Jun 1, 2013 at 0:42

Hi this may be a solution to your problem. Instead of putting:

<?php echo $embedUrl; ?>

Put it like this:

<?php echo substr($embedUrl, 0, 11); ?>

it will echo only the 11 character code after the v= of the extracted Youtube link on the video post content. I have tested it myself and came with this solution. Hope this helps!

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.