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

| Views: 910
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

Have comments or feedback on what I wrote? Please share them below! Found this useful? Consider sending me a small tip.
comments powered by Disqus
Other posts you may like...