agnes the alien!

Creating An Automatically Updating Sitemap In PureBlog

I wanted to not have to update my sitemap every time I made a new page, so here's my VERY basic automatically updating Sitemap page for Pureblog. I altered THIS CODE SNIPPET by David Carr for it.

Basically, this takes a list of the files in your pureblog/content/pages directory and retrieves it for the page with its title. Note that I'm using my dockerized version so please edit accordingly. When I learn more PHP, I will be updating this with categories and alphabetical sorting. For now though:

  1. Make a file called sitemap-auto.php in content/includes/

  2. Make a custom route in settings

/sitemap-auto | /content/includes/sitemap-auto.php

  1. Copy the code below or here

require PUREBLOG_BASE_PATH . '/includes/header.php';
render_masthead_layout($config, ['page' => null]);

?>

<main>
<h1> Sitemap</h1>

<?php

$directory = '/var/www/html/content/pages/';

$files = scandir($directory);

echo "<ul>";

foreach ($files as $file) {
    if ($file !== "." && $file !== "..") {
        $name = pathinfo($file, PATHINFO_FILENAME);
  $path = $directory . '/' . $file;
    $contents = file_get_contents($path);

       preg_match('/^title:\s*(.+)$/m', $contents, $matches);

    $title = $matches[1] ?? pathinfo($file, PATHINFO_FILENAME);

    $url = '/' . pathinfo($file, PATHINFO_FILENAME);

    echo '<li>🛸 <a href="' . htmlspecialchars($url) . '">'
        . htmlspecialchars(trim($title))
        . '</a></li>';
}
}
echo "</ul>";
?>

</main>
<?php render_footer_layout($config, ['page' => null]); ?>
</body>
</html>
  1. Put it in /content/includes/sitemap-auto.php

You're done :)

This will automatically update when a new file is in the pages list. You can find a live preview on my site here.

tech, tutorial, meta

⬅ Previous post
Weekly Book Thoughts: Fruit Fly - Josh Silver

Next post âž¡
Creating A Stats Page In PureBlog