How to add dynamic single page URLs to Concrete5 sitemap.xml

At some point while working with single pages in Concrete5, you may come across the need to add your own URLs to the sitemap.xml file. So that search engines are aware of the pages you have made. It doesn’t have to be for a single page however. This guide is quite simply how to manually add pages to your sitemap.xml file the correct Concrete5 way.

This guide is for Concrete5 version 8. It will not work on older versions and may stop working in newer version.

Where to Put Your Code

Concrete5 comes with a file that you can edit to do all sorts of things. You can find it under /application/bootstrap/app.php. Open this file and have a read. It contains some example code of various different things you can achieve with the file.

Example Code

Concrete5 has a built in listener for when the sitemap.xml file is generated. It is called on_sitemap_xml_ready. This is where you will do your manual changes to the sitemap.xml file.

Put this code in your /application/bootstrap/app.php anywhere after the starting <?php tag.

Events::addListener('on_sitemap_xml_ready', function($event) {

    // get the sitemap
    $sitemap = $event->getSubject()['xmlDoc'];

    // put your additional sitemap.xml URLs below
    $url = $sitemap->addChild('url');
    $url->addChild('loc', BASE_URL . '/path-to-your-page');
    $url->addChild('lastmod', date('Y-m-d\TH:i:sP', time()));
    $url->addChild('changefreq', 'monthly');
    $url->addChild('priority', '0.5');

});

Adjusting Your Code

The above code will need some adjustment.

To start with you will need to change the loc value from /path-to-your-page to the actual address you would like added to your sitemap.xml. This is quite straight forward.

Likewise lastmod value should not be time(),¬†as it is in the above example. Instead you should use the actual last modified date of the page you are linking to. So that search engines don’t waste precious time on that page instead of another page of your website.

Same goes for your changefreq and priority. Use what is best for your website. The code example has sensible default values if you do not know what to put here.

Lastly, repeat the lines of code that start with $code for as many pages you would like to add. You might want to put the code inside a loop.

Generate the sitemap.xml

Updating this file alone will not update your sitemap.xml file. You will need to run the job within the Concrete5 dashboard. Under “System & Settings -> Automated Jobs” run the “Generate the sitemap.xml” file task.

After you have done this, your sitemap.xml file should be updated. If you do not want to have to do this all the time, be sure to set up a cron job. How you do this will depend on your website host, and is unfortunately beyond the scope of this article.

Conclusion

As you can tell, making manual changes to your sitemap.xml in Concrete5 is very easy. There isn’t much else to add. Hope you found this article helpful!