If you have a lot of video content on the web, you will want to create a video sitemap to submit to Google and the other search engines. Here is a step-by-step tutorial on how to create a video sitemap with WordPress.
Some of these steps might not be appropriate for your situation, but I will start at the point where you have your video files (e.g. .M4V, .MP4, .MOV, .FLV, etc…) on a publicly-accessible web server somewhere and you have a fresh empty installation of WordPress also on a publicly-accessible web server.
<!-- create a Video Sitemap template -->
<?php
/*
Template Name: Video Sitemap
*/
<!-- create a variable called $posts -->
$posts = query_posts(array_merge(
array(
'cat' => 'video',
'posts_per_page' => 9999,
'meta_key' => 'video_url'
)
));
<!-- this is the namespace of the XML file and the top of the sitemap -->
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
';
?>
<!-- loop to get each video and thumbnail URL, one at a time -->
<?php foreach ($posts as $post) {
$hosted_file_name = get_post_meta($post->ID, 'video_url', true);
if ( has_post_thumbnail() ) {
$videothumbnail = get_the_post_thumbnail ($post->ID);
}
?>
<!-- get video URL as its own element node along with the video attributes - change the spaceholder URLs -->
<url>
<loc><?php echo the_permalink($post->ID); ?></loc>
<video:video>
<video:thumbnail_loc><?php echo $videothumbnail ?></video:thumbnail_loc>
<video:title><?php the_title(); ?></video:title>
<video:description><?php the_excerpt(); ?></video:description>
<video:content_loc><?php echo $hosted_file_name; ?></video:content_loc>
<video:player_loc>http://www.yourwebsite.com/file/path/yourflashplayer.swf</video:player_loc>
<video:publication_date><?php echo get_the_date('Y-m-d'); ?></video:publication_date>
<video:category>Here write the main content category of all of your videos</video:category>
<?php
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo '<video:tag>' . $tag->name . '</video:tag> ';
}
}
?>
<!-- this should be set to "no" if these are not suitable for kids -->
<video:family_friendly>yes</video:family_friendly>
</video:video>
</url>
<?php } ?>
</urlset>
There should now be a video sitemap at http://www.yoursite.com/video-sitemap/. You can submit this URL to Google Webmaster Tools as a sitemap.
August 31st, 2012 at 8:39 am
Looks like a very good and straight forward solution in comparision to clunky and unnecessary plugin bloats. However, the xml file i have created does not appear in the page-template dropdown.
Is it because it is not a php file ? Please shed some light.
Cheers….
August 31st, 2012 at 9:53 am
Bunty
There was a mistake in my instructions. Name the file that you put inside your theme video-sitemap.php, not video-sitemap.xml.
I have corrected the mistake above. See if that works.
Burke
September 1st, 2012 at 2:32 am
Hello burke,
thank you, the solution worked perfectly. I really appreciate your quick feedback. There was a just a minor glitch though on line 25. the code
$videothumbnail = get_post_thumbnail ($post->ID);, threw an error “call to undefined function”. However when replaced withthe_post_thumbnail, it works perfectly. Any thoughts on that ?My major concern however is that google expects a sitemap with a .xml extension instead of an html page, any workaround to that?
Other that that it works like charm. Thank you for taking your time for this tutorial. It is very unique and helpful.
Cheers.
Bunty.
September 1st, 2012 at 3:42 am
sorry..Just a quick correction, I replaced
get_post_thumbnailwithget_the_post_thumbnailand it worked as expected. However I am still clueless on how to render a .xml sitemap? Any help will be truly appreciated. Thanks.September 1st, 2012 at 9:33 am
Perhaps you could rewrite the URL in .htaccess to display it as video-sitemap.xml instead of /video-sitemap/. I’m not exactly sure of the exact code, but perhaps it would be in the same way that WordPress creates permalinks.
September 1st, 2012 at 9:36 am
Thanks for the edit. I have replaced get_post_thumbnail with get_the_post_thumbnail above.