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.

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
  }
}

?>
share|improve this question
    
Where does that code fail? –  s_ha_dum Jul 28 '13 at 19:56
    
Everywhere where I put it in... It does not embed the video, although it shows me something like a video (a white background). But it's not getting all video's at all... –  Johan Jul 28 '13 at 20:00
1  
You missed the point. I am trying to prompt you to do some of your own debugging. Does the query fail? Does the preg_match fail? Where does this go wrong? –  s_ha_dum Jul 28 '13 at 20:04
    
If I'd know that, I wouldn't ask that and would have solved it myself. Don't know what is going wrong, but there is going something wrong, that's for sure. –  Johan Jul 28 '13 at 20:14
1  
Use var_dump to dump your variables and see what is actually happening-- basic low-tech PHP debugging. –  s_ha_dum Jul 28 '13 at 20:17

2 Answers 2

Without even running that code I can see a pretty significant problem. You describe the goal as to "check all my posts to see if there is a embedded YouTube video" but the query is restricted to only one post.

And then, you are only attempting to use the very first post in the results (if the query works at all) anyway so there is no way that this is going to get all the YouTube videos from all the posts. If it is lucky it will get the videos from one post only. The first thing you need to do is Loop through all the posts. Untested, but something like this:

$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);
  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
  }
}

Your regex is also wrong. YouTube video IDs have underscores.

Put altogether:

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

  $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);
    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
    }
  }
}
share|improve this answer
    
When I use this: function aihato_latest_video() { $query_args = array( 's' => 'youtube.com/watch?v=', 'posts_per_page' => 1 ); AND HERE GOES YOUR CODE it says unexpected end –  Johan Jul 28 '13 at 20:22
    
You didn't close the function with a }. –  s_ha_dum Jul 28 '13 at 20:29
    
Check my edit, I tried that, but still the same... –  Johan Jul 28 '13 at 20:37
    
You still didn't close the function correctly. See the edit. –  s_ha_dum Jul 28 '13 at 20:44
    
Thanks, I put your code in functions.php and used <?php aihato_latest_video(); ?> in tag.php to try it, but nothing happened at all... –  Johan Jul 28 '13 at 20:48

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.

share|improve this answer
    
Thanks Pontus, but I get this error: array_keys() expects parameter 1 to be array, boolean given –  Johan Jul 28 '13 at 20:32
2  
I do like your general idea. But hooking a function to the save_post action that retrieves and iterates over all posts? Every time a post is created, updated, autosaved? That is humongous overhead! –  Johannes Pille Jul 28 '13 at 20:37
    
@Pontus Abrahamsson: Your edit does not give me errors, but it does not show anything either... –  Johan Jul 28 '13 at 20:40
1  
@JohannesPille yeah ofcourse! im just tired.. hehe, now it saves the current edited/published posts and looks for videos. johan, edit a post that have a video and save it again, and add the get_youtube_archive() function where you want the videos. –  Pontus Abrahamsson Jul 28 '13 at 20:47
1  
@PontusAbrahamsson : That is basically what I said, but you do need to query all the posts to catch any videos in previously saved posts. After that you only need to check the current one. –  s_ha_dum Jul 28 '13 at 21:03

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.