WordPress, PHP – How can I check whether the single post contains youtube video embedded or not?

So I am trying to have quick boolean check to see if the blog post, which is single.php contains any youtube video embedded, and if it does, then display an video embedded image on the home.php page.

I thought the best way to approach is by string finder using php since all youtube video embedded contains HTML that looks like this:

Read More
inframe src="http://www.youtube.com/embed/...

However, the thing is I have no idea where to start out.. If someone could help me how to start out, that will be great! Thank you!

Related posts

Leave a Reply

2 comments

  1. If all the embeded youtube videos are iframes, this should work:

    <?php $content = get_the_content();
    preg_match  ('/<iframe(.+)"/', $content, $matches);
    if ($matches !=""){
    $hasiframe = true;}
    ?>
    <?php if ($hasiframe)
    {
    //do something
    } else {
    //do something else
    } ?>
    
  2. Why don’t you just use a Blog Category called “with video” and then run a loop with just that cat on the home page?

    $query = new WP_Query( 'cat=2' ); //Assuming "id 2" is the category with videos