Get all Youtube video’s from posts and embed them on a different page

Is this possible? I want to let it check all my posts to see if there is a embedded Youtube video. If so, I want to show that on a different page, embedded and linking to that particular post…

I tried the following snippet, but that did not work for me. It doesn’t embed a video…

function aihato_latest_video() {
  $query_args = array(
    's' => 'youtube.com/watch?v=',
    'posts_per_page' => 1
  );

  $posts = get_posts( $query_args );
  $post1 = $posts[0];

  $matches = array();

  preg_match('|http://www.youtube.com/watch\?v=([a-zA-Z0-9]+)|', $post1->post_content, $matches);
  $v = $matches[1];
?>
<object width="415" height="250">
  <param name="movie" value="http://www.youtube.com/v/<?php echo $v ?>&hl=en_US&fs=1&"></param>
  <param name="allowFullScreen" value="true"></param>
  <param name="allowscriptaccess" value="always"></param>
  <embed src="http://www.youtube.com/v/<?php echo $v ?>&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="415" height="250"></embed>
</object>
<?php
}

Edit: This is working neither:

function aihato_latest_video() {
  $query_args = array(
    's' => 'youtube.com/watch?v=',
    'posts_per_page' => 1
  );

$posts = get_posts( $query_args );
foreach ($posts as $p) {
  $matches = array();
  preg_match('|http://www.youtube.com/watch\?v=([a-zA-Z0-9]+)|', $p->post_content, $matches);
?>

 <?php if (!empty($matches[1])) { ?>
    <object width="415" height="250">
      <param name="movie" value="http://www.youtube.com/v/<?php echo $matches[1] ?>&hl=en_US&fs=1&"></param>
      <param name="allowFullScreen" value="true"></param>
      <param name="allowscriptaccess" value="always"></param>
      <embed src="http://www.youtube.com/v/<?php echo $matches[1] ?>&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="415" height="250"></embed>
    </object><?php
  }
}

?>

2 Answers
2

Another method can be like this, On the current post when you save look for youtube video in the content, if found save it to the transient 'videos' and the post_id (for the link to the post later) insted of regex im using parse_url() and parse_str()

add_action( 'save_post', 'save_youtube_videos' );
function save_youtube_videos( $post_id ) 
{
    $saved   = get_transient( 'videos' ); // If we have videos before
    $videos  = !empty( $saved ) ? $saved : array();
    $content = isset( $_POST['post_content'] ) ? $_POST['post_content'] : null;

    if( $content )
    {
        parse_str( parse_url( $content, PHP_URL_QUERY ), $video );
        if( !in_array( $video['v'], $videos ) )
        {
            $videos[ $post_id ] = $video['v']; // Push post_id and the vide_id
            set_transient( 'videos', $videos );  // Save all videos as array
        }
    }
}

Save your post_id and youtube_id if your post have one. So on update or publish look for a video using parse_str and parse_url, if we find one add it to the transient cache so we dont need to find all the posts again.

function get_youtube_archive( $width="415", $height="250" )
{
    $videos = get_transient( 'videos' );

    if( !empty( $videos ) && count( $videos ) > 0 )
    {
        $output="";

        foreach( $videos as $key => $value ) 
        {
            $output .= '<object width="'. $width .'" height="'. $height .'">';
                $output .= '<param name="movie" value="http://www.youtube.com/v/'. $value .'/&hl=en_US&fs=1&"></param>';
                $output .= '<param name="allowFullScreen" value="true"></param>';
                $output .= '<param name="allowscriptaccess" value="always"></param>';
                $output .= '<embed src="http://www.youtube.com/v/'. $value .'&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'. $width .'" height="'. $height .'"></embed>';
            $output .= '</object>';

            $output .= '<a href="'. get_permalink( $key ) .'" alt="Go to post">Go to post</a>';
        }

        return $output;
    }
}

And a listing function so you can get the videos by just add:

echo get_youtube_archive();

Where you want them. You have to save the posts where you have videos for this to work.

Leave a Comment