Source: templates/google-news-sitemap.php

  1. <?php
  2. /**
  3. * Sitemap template
  4. *
  5. * @package simple-google-news-sitemap
  6. */
  7. use SimpleGoogleNewsSitemap\CacheUtils;
  8. $links = CacheUtils::get_cache();
  9. /**
  10. * Filter all items that will be output in the sitemap.
  11. *
  12. * @since 1.0.0
  13. *
  14. * @hook simple_google_news_sitemap_data
  15. * @param {array} $links Array of items to be output.
  16. * @returns {array} Array of items to be output.
  17. */
  18. $links = apply_filters( 'simple_google_news_sitemap_data', $links );
  19. // Used for publication name and language.
  20. $publication = get_bloginfo( 'name' );
  21. $language = get_bloginfo( 'language' );
  22. if ( empty( $links ) ) {
  23. $links = [];
  24. }
  25. header( 'Content-type: application/xml; charset=UTF-8' );
  26. echo '<?xml version="1.0" encoding="UTF-8"?>';
  27. ?>
  28. <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
  29. <?php
  30. /**
  31. * Add extra data to the start of the sitemap.
  32. *
  33. * @since 1.0.0
  34. * @hook simple_google_news_sitemap_start
  35. */
  36. do_action( 'simple_google_news_sitemap_start' );
  37. foreach ( $links as $link ) :
  38. if ( empty( $link['url'] ) || empty( $link['title'] ) ) {
  39. continue;
  40. }
  41. // Remove empty space from the beginning & end of title.
  42. $title = trim( str_replace( '&nbsp;', ' ', $link['title'] ) );
  43. ?>
  44. <url>
  45. <loc><?php echo esc_url( $link['url'] ); ?></loc>
  46. <news:news>
  47. <news:publication>
  48. <news:name><?php echo esc_html( $publication ); ?></news:name>
  49. <news:language><?php echo esc_html( $language ); ?></news:language>
  50. </news:publication>
  51. <news:publication_date><?php echo esc_html( date( DATE_W3C, $link['modified'] ) ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date ?></news:publication_date>
  52. <news:title><?php echo esc_html( $title ); ?></news:title>
  53. </news:news>
  54. </url>
  55. <?php
  56. endforeach;
  57. /**
  58. * Add extra data to the end of the sitemap.
  59. *
  60. * @since 1.0.0
  61. * @hook simple_google_news_sitemap_end
  62. */
  63. do_action( 'simple_google_news_sitemap_end' );
  64. ?>
  65. </urlset>