Skip to content

Instantly share code, notes, and snippets.

@CrisHazzard
Created October 21, 2016 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CrisHazzard/a50e9161ebea6c3ae4ac51cf899eb7f8 to your computer and use it in GitHub Desktop.
Save CrisHazzard/a50e9161ebea6c3ae4ac51cf899eb7f8 to your computer and use it in GitHub Desktop.
Video Sitemap using ACF field for YouTube Video on HikingGuy.com
// ****************************************************************************************************
// Video Sitemap
add_action( "save_post", "eg_create_sitemap" );
function eg_create_sitemap() {
// youtube_video is the ACF video where I store the normal YouTube link
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'youtube_video',
'value' => '',
'compare' => '!=',
),
),
);
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ):
$sitemap = '<?xml version="1.0" encoding="UTF-8"?>';
$sitemap .= "\n" . '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"> ' . "\n";
while( $the_query->have_posts() ) : $the_query->the_post();
preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", get_field('youtube_video') , $matches);
$videoID = $matches[0];
// Enter your API key in this string
$json_url = 'https://www.googleapis.com/youtube/v3/videos?id=' . $videoID . '&key=[your key]&fields=items(id,snippet,contentDetails,statistics)&part=snippet,contentDetails,statistics';
$json = file_get_contents($json_url);
$json_output = json_decode($json);
if($json_output->items[0]->statistics->viewCount){
$viewCount = $json_output->items[0]->statistics->viewCount;
$title = $json_output->items[0]->snippet->title;
$duration = $json_output->items[0]->contentDetails->duration;
$duration = ISO8601ToSeconds($duration);
}
$postdate = explode( " ", $post->post_modified );
$sitemap .= "\t" . '<url>' . "\n" .
"\t\t" . '<loc>' . get_permalink( $post->ID ) . '</loc>' .
"\n\t\t" . '<video>' .
"\n\t\t\t" . '<video:thumbnail_loc>https://i.ytimg.com/vi/' . $videoID . '/hqdefault.jpg</video:thumbnail_loc>' .
"\n\t\t\t" . '<video:title><![CDATA[' . $title . ']]></video:title>' .
"\n\t\t\t" . '<video:description><![CDATA[' . get_the_excerpt($post->ID ) . ']]></video:description>' .
"\n\t\t\t" . '<video:player_loc allow_embed="yes" autoplay="ap=1">' . get_field('youtube_video') . '</video:player_loc>' .
"\n\t\t\t" . '<video:duration>' . $duration . '</video:duration>' .
"\n\t\t\t" . '<video:view_count>' . $viewCount . '</video:view_count>' .
"\n\t\t\t" . '<video:rating>5.0</video:rating>' .
"\n\t\t\t" . '<video:category><![CDATA[Travel & Events]]></video:category>' .
"\n\t\t" . '</video>' .
"\n\t" . '</url>' . "\n";
endwhile;
$sitemap .= '</urlset>';
endif;
$fp = fopen( ABSPATH . "video-sitemap.xml", 'w' );
fwrite( $fp, $sitemap );
fclose( $fp );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment