Wordpress page template to search and display videos from youtube

Wordpress page template to search videos in youtube using youtube api

Following sample wordpress page template code is designed to search videos in youtube using youtube api. Please sign up and replace with your api key in below code.
<?php /** * @package WordPress * @subpackage Default_Theme */ /* Template Name: Video-search */ ?> <?php get_header(); ?> <div class="col1"> <?php if (have_posts()) : ?> <b><u>Search your favourite video from youtube </u></b><br><hr style="height:2px;border-width:0;color:gray;background-color:gray" /> <br> <form action="http://www.indiacitys.com/videosearch" method="post"> Search term : <input type="text" name="srchkeyinput" /><br><br> <input type="submit" name="videosrch" value="Submit Form" /> </form> <?php $searchkey = 'India news'; $searchkey = urlencode($searchkey); if(isset($_POST['videosrch'])) { $searchkey = urlencode($_POST["srchkeyinput"]); } $videorequesturl = 'http://gdata.youtube.com/feeds/api/videos?q='.$searchkey.'&max- results=50&category=News&orderby=published&key=AI39si6SidOUpvJexNqwDRl1fOb3HD1Ic4cx_zwlqycDIyKNte5b3TbtjeDRL3Um1bZSFXIXXmitnfIGEdi1_GnRFFW8J5T3gQ'; $string = file_get_contents($videorequesturl); $string=str_replace('media:',"media_",$string); $string=str_replace('yt:',"yt_",$string); $errormsg = 'error in submitted query'; if (strpos($http_response_header[0], "200")) { //$xml = new SimpleXMLElement($string); $xml=simplexml_load_string($string); echo "<hr style='height:2px;border-width:0;color:gray;background-color:gray' />"; echo "Search results for your query='<b>".$searchkey."</b>'<hr style='height:2px;border-width:0;color:gray;background-color:gray' />"; echo "<table border='1' style='height:2px;border-width:0;color:gray;background-color:gray'>"; $colcount = 0; echo "<tr>"; foreach ($xml->entry as $entry) { if ($colcount == 4) { $colcount = 0; echo "</tr>"; echo "<tr>"; } $link=$entry->link; $pdate=strtotime($published); $ddate=date('Y-m-d H:i:s',$pdate); $media=$entry->media_group; $murl=$media->media_content['url']; $murl2=$media->media_player['url']; $media_title=$media->media_title; // short description $media_thumbnail=$media->media_thumbnail['url']; echo "<td>"; echo "<a href='".$murl2."'target='_blank' >".$media_title."</a>"; echo "<br>"; echo "<a href='".$murl2."'target='_blank' > <img src='".$media_thumbnail."' alt='".$media_title."' WIDTH=200 HEIGHT=157 /> </a>"; echo "<br>"; echo "</td>"; $colcount++; } } else { echo "<tr><td><b>Error Message: ".$errormsg."</b></tr></td>"; } echo "</tr></table><hr style='height:2px;border-width:0;color:gray;background-color:gray' />"; ?> <?php endif; ?> </div><!--/col1--> <?php get_footer(); ?>
Sign In or Register to comment.