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
}
}
?>
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()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.
And a listing function so you can get the videos by just add:
Where you want them. You have to save the posts where you have videos for this to work.
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:
Your
regex
is also wrong. YouTube video IDs have underscores.Put altogether: