Last active
August 29, 2015 14:05
-
-
Save AmmarCodes/c28afd0a4068f23d5ee1 to your computer and use it in GitHub Desktop.
WordPress: Add Google News Sitemaps using WordPress SEO, for more explanation check the blog post: https://aalakkad.github.io/blog/wordpress-add-google-news-sitemaps/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
////////////////////////////////////////////////////////////////////////////////// | |
// Google News Sitemap // | |
// // | |
// Refer to: https://aalakkad.github.io/blog/wordpress-add-google-news-sitemaps // | |
////////////////////////////////////////////////////////////////////////////////// | |
// Check for WPSEO_Sitemaps class from WordPress SEO plugin | |
if(class_exists('WPSEO_Sitemaps')) { | |
add_action('init', 'my_custom_google_news_sitemap'); | |
function my_custom_google_news_sitemap() { | |
$GLOBALS['wpseo_sitemaps']->register_sitemap( 'google-news-sitemap', function() { | |
$query_args = [ | |
'post_type' => 'post', | |
'post_status' => 'publish', | |
'posts_per_page' => 1000, // Google bots won't crawl more than 1000 entries per sitemap | |
'paged' => 1 | |
]; | |
$posts = get_posts($query_args); | |
$site_title = get_bloginfo( 'name' ); // get site's name | |
$language = get_bloginfo( 'language' ); // get site's language (e.g. en or ar) | |
$xml = '<?xml version="1.0" encoding="UTF-8"?> | |
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" | |
xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">'; | |
foreach($posts as $post) { | |
$permalink = get_permalink($post->ID); | |
$title = $post->post_title; | |
$date = get_the_time( 'c', $post->ID ); // c is the complete date plus hours, minutes, seconds and a decimal fraction of a second which is accepted by Google | |
$genres = 'PressRelease'; // Comma-seperated geners | |
$keywords = 'comma, seperated, keywords'; // Comma-seperated keywords | |
$xml .= <<<XML | |
<url> | |
<loc>{$permalink}</loc> | |
<news:news> | |
<news:publication> | |
<news:name>{$site_title}</news:name> | |
<news:language>{$language}</news:language> | |
</news:publication> | |
<news:genres>{$genres}</news:genres> | |
<news:publication_date>{$date}</news:publication_date> | |
<news:title>{$title}</news:title> | |
<news:keywords>{$keywords}</news:keywords> | |
</news:news> | |
</url> | |
XML; | |
} | |
$xml .= '</urlset>'; | |
echo $xml; | |
// Remove any wp_footer actions and die() | |
$GLOBALS['wpseo_sitemaps']->sitemap_close(); | |
}, 'sitemap_news\.xml'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment