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 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; ?>                       
<?php
    $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--> 
        <?php
        $args = array(
            'post_type' => 'post', // if the post type is post 
            'posts_per_page' => 1,
            'tax_query' => array(
            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>
        </div>
    <?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.

share|improve this question
    
parse_url(get_the_content()) expects a plain URL. Anything else will break it. –  toscho May 28 '13 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 '13 at 2:19

1 Answer 1

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
}
else
{
    // echo $content or something else
}
share|improve this answer
    
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 '13 at 0:42

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.