Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

NOTE: This article is 3 years or older so its information may no longer be relevant. Read on at your own discretion! Comments for this article have automatically been locked, refer to the FAQ for more details.
I could not find a FlatPress sitemap generator, the one that was available previously seems to be down now, so I've decided to write my own based on some of the SQL export code on the FlatPress wiki.

Here's the result, place this in the same directory as index.php and call it sitemap.php.

This script will generate a Google compatible sitemap for the whole blog.

There are some limitations:
  • Will not work if there are more than 50,000 entries in the blog
  • Categories are not included
  • Each blog entry last updated date is the blog entry date, not the date of the last comment for the entry

 sitemap.php
<?php
require_once('defaults.php');
require_once(INCLUDES_DIR.'includes.php');
if (function_exists('system_init')) {
system_init();
}
else {
plugin_loadall();
}
header('Content-Type: text/xml; charset=utf-8');
error_reporting(E_ALL);
echo("<?");
?>xml version="1.0" encoding="UTF-8"<?php echo("?>"); ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc><?=BLOG_BASEURL?></loc>
<lastmod><?=date("c");?></lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<?
$q = new FPDB_Query(array('start'=>0, 'count'=>-1, 'fullparse'=>true), null);
while($q->hasMore()) {
list($id, $e) = $q->getEntry();
$offset = $fp_config['locale']['timeoffset'];
if (isset($e['lastupdate'])) {
$lastmod = $e['lastupdate'] - (60 * 60 * $offset);
}
else {
$lastmod = $e['date'] - (60 * 60 * $offset);
}
$loc = BLOG_BASEURL . "index.php?x=entry:" . $id;
?>
<url>
<loc><?=$loc?></loc>
<lastmod><?=date("c", $lastmod);?></lastmod>
</url>
<?php
}
?>
</urlset>



Edit: the original version of this code didn't take the time offset into account, I've updated it now to use the offset. Without the offset, the URLs are not created correctly sometimes.

Edit 2: changed the locations to use permalinks instead of the pretty URLs because I've noticed a large number of the URLs were not being generated correctly due to the time offsets.

Edit 3: the escape slashes got stripped out in the original code, I've changed the code so escapes are not required.

-i

Skip down to comments...
A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. If you spot something out of place, please do let me know.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
 
comments powered by Disqus
Other posts you may like...