Creating a Google News Sitemap in PHP

Alexandr Berezhnyk
09-22 07:00
  1. PHP
  2. SEO
  3. Google
Creating a Google News Sitemap in PHP

Google provide a lot of new features in sitemap, which must help scan our sites better than ever. One of that features is Google News Sitemap - now we can say Google, what our blog's posts exactly are news, set genre of a post and even set post keywords in our sitemap.

How start to use Google News Sitemap in my sitemap.xml?

It's not so hard, as you may think. First: here is an official doc. I will concentrate the attention of this post on technical part, so you can read all terminology in the documentation.

Here is the basic sitemap.xml example:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>http://www.mySite.com/home/index</loc>
  </url>
  <url>
    <loc>http://www.mySite.com/about/index</loc>
  </url>
  <url>
    <loc>http://www.mySite.com/news/index</loc>
    <lastmod>2015-09-22</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.8</priority>
  </url>
</urlset>

In last url item you can see 3 optional params:

  • lastmod - date of last page modification
  • changefreq - insensitivity of page changing
  • priority - what is the priority of this page to other pages on you site

That's simple: we can store all site pages, set which pages are important and e.g. Now if we want to use Google News Sitemap specification, we must include it namespace into our xml. Here is needed namespace:

xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"

And here is how we can include it:

<?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" >

  <url>...</url>
  <url>...</url>
  <url>...</url>

</urlset>

After this step, we can create XML tags like news:publication_date. If you want to create you sitemap without any PHP code - it will not hard to make this tags. But here, in PHP we must do more steps to work with that namespace. Below you will see it.

a Google News Sitemap and PHP: how to do it?

For work with XML in PHP, I will like to use simplexml functions. So, for example, I have news.xml, which is an empty sitemap file:

<?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" >
</urlset>

And I have array list - it's the list of my news:

$list = [
  [
    'id'=>'1',
    'title'=>'Web Flat Design UI Kit',
    'keywords'=>'Web Flat Design,UI Kit',
    'date'=>'2015-09-21'
  ],
  [
    'id'=>'2',
    'title'=>'How to protect your source code python-application?',
    'keywords'=>'Python protection',
    'date'=>'2015-09-22'
  ],
  [
    'id'=>'3',
    'title'=>'Creating a Google News Sitemap in PHP',
    'keywords'=>'Google New Sitemap, Google sitemap.xml in PHP',
    'date'=>'2015-09-22'
  ]
];

In sitemap.xml each of this array item will be a url tag, inside of it will be loc and others tags. For do this, first I want to load my XML in some PHP variable.

// store filename in variable
// I use this name in 2 places in code:
//   open and save change in xml file
$fileName = 'news.xml';
// open xml
$sitemap = simplexml_load_file($fileName);

Now I can edit news.xml as I want, but changes will not add before I will not save them. For example:

$fileName = 'news.xml';
$sitemap = simplexml_load_file($fileName);
$url = $sitemap->addChild('url','');
$url->addChild('loc','http://site.com/home/index');
$url->addChild('priority','1.0');
// save changes
$sitemap->asXml($fileName);

It's was an easy example of that, what we will do now. As you can see above, we register news namespace in urlset of our XML file. In PHP, if I want to create some XML tags in the namespace, I must register it before. In out case here is registration on news namespace:

$news = 'http://www.google.com/schemas/sitemap-news/0.9';
$sitemap->registerXPathNamespace('news', $news);

And when I create tag with this namespace, I must set it as the third argument in SimpleXML addChild function:

// will create <news:example></news:example>
$example = $sitemap->addChild('news:example', '', $news);

Now let's create and save news items in our sitemap using all PHP features, which you saw above.

$list = [
  [
    'id'=>'1',
    'title'=>'Web Flat Design UI Kit',
    'keywords'=>'Web Flat Design,UI Kit',
    'date'=>'2015-09-21'
  ],
  [
    'id'=>'2',
    'title'=>'How to protect your source code python-application?',
    'keywords'=>'Python protection',
    'date'=>'2015-09-22'
  ],
  [
    'id'=>'3',
    'title'=>'Creating a Google News Sitemap in PHP',
    'keywords'=>'Google New Sitemap, Google sitemap.xml in PHP',
    'date'=>'2015-09-22'
  ]
];

$fileName = 'news.xml';
$sitemap = simplexml_load_file($fileName);

$news = 'http://www.google.com/schemas/sitemap-news/0.9';
$sitemap->registerXPathNamespace('news', $news);

for($i=0,$c=count($list);$i<$c;$i++){
  # create new xml node
  $url = $sitemap->addChild('url','');
  # add link to news
  $url->addChild('loc', 'http://www.devbattles.com/blog/post/'.$list[$i]['id']);
  # set post priority
  $url->addChild('priority', '0.5');
  # creare news tags
  $newsNode = $url->addChild('news:news', '', $news);
  # publication part
  $pub = $newsNode->addChild('news:publication', '', $news);
  $pub->addChild('news:name', htmlspecialchars($list[$i]['title']), $news);
  $pub->addChild('news:language', 'en', $news);
  # other details
  $newsNode->addChild('news:publication_date', $list[$i]['date'], $news);
  $newsNode->addChild('news:type','Blog', $news);
  $newsNode->addChild('news:keywords',$list[$i]['keywords'], $news);
}

// save all changes
$sitemap->asXml($fileName);

And now, our news.xml looks like the code below:

<?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" >

<url>
  <loc>http://www.devbattles.com/blog/post/1</loc>
  <priority>0.5</priority>
  <news:news>
    <news:publication>
      <news:name>Web Flat Design UI Kit</news:name>
      <news:language>en</news:language>
    </news:publication>
    <news:publication_date>2015-09-21</news:publication_date>
    <news:type>Blog</news:type>
    <news:keywords>Web Flat Design,UI Kit</news:keywords>
  </news:news>
</url>
<url>
  <loc>http://www.devbattles.com/blog/post/2</loc>
  <priority>0.5</priority>
  <news:news>
    <news:publication>
      <news:name>How to protect your source code python-application?</news:name>
      <news:language>en</news:language>
    </news:publication>
    <news:publication_date>2015-09-22</news:publication_date>
    <news:type>Blog</news:type>
    <news:keywords>Python protection</news:keywords>
  </news:news>
</url>

<url>
  <loc>http://www.devbattles.com/blog/post/3</loc>
  <priority>0.5</priority>
  <news:news>
    <news:publication>
      <news:name>Creating a Google News Sitemap in PHP</news:name>
      <news:language>en</news:language>
    </news:publication>
    <news:publication_date>2015-09-22</news:publication_date>
    <news:type>Blog</news:type>
    <news:keywords>Google New Sitemap, Google sitemap.xml in PHP</news:keywords>
  </news:news>
</url>

</urlset>

That was all what you must to know for successful developing a Google news sitemap for your site using PHP. Thanks for reading.

Want to get a great job? Add CV