add categories to sitemap plugin through the renderSitemap hook I'm trying to setup a sitemap using the craft-sitemap plugin, however it only covers sections by default and I need to add category groups into it, as half of the URLs are in categories.

I can see the Service Methods section in the docs that says that "There’s several service methods made available to add items to the sitemap."

Hooks
renderSitemap

Add a renderSitemap method to your plugin to add items via the various service methods listed below.

Here’s an example plugin hook method with comments:

addCategoryGroup(CategoryGroupModel $categoryGroup, [$changefreq, [$priority]])


Adds all categories in the group to the sitemap.

$group = craft()-&gt;categories-&gt;getGroupByHandle('news');
craft()-&gt;sitemap-&gt;addCategoryGroup($group);


But I'm not sure where to start. Where does the hook code go? As I'm assuming I'd have to create a new file, as it will need to be separate from the sitemap plugin as that will be overwritten if there are any updates.

I would appreciate some advice on this please?
You'll need to create a separate plugin, which adds the categories to the sitemap in response to the renderSitemap hook.

Here's what your main plugin file will look like. This should get you 90% of the way there; the rest is just organising a few files and folders, which is well-covered by the official documentation.

&lt;?php

/**
 * SitemapCategories
 *
 * @package Craft
 */

namespace Craft;

/**
 * The main SitemapCategories plugin file.
 */
class SitemapCategoriesPlugin extends BasePlugin
{
    /**
     * Returns the plugin name.
     *
     * @return string
     */
    public function getName()
    {
        return Craft::t('Sitemap Categories');
    }

    /**
     * Returns the plugin description.
     *
     * @return string
     */
    public function getDescription()
    {
        return Craft::t('Adds categories to the sitemap.');
    }

    /**
     * Returns the plugin’s version number.
     *
     * @return string The plugin’s version number.
     */
    public function getVersion()
    {
        return '0.1.0';
    }

    /**
     * Returns the plugin developer’s name.
     *
     * @return string The plugin developer’s name.
     */
    public function getDeveloper()
    {
        return 'Your name here';
    }

    /**
     * Returns the plugin developer’s URL.
     *
     * @return string The plugin developer’s URL.
     */
    public function getDeveloperUrl()
    {
        return 'https://yourwebsite.com';
    }

    /**
     * Returns a faux schema version, so Craft doesn't attempt to run database
     * updates when the plugin version changes.
     *
     * @return string
     */
    public function getSchemaVersion()
    {
        return '0.0.0';
    }

    /**
     * Returns a boolean indicating whether the plugin has settings.
     *
     * @return bool
     */
    public function hasSettings()
    {
        return false;
    }

    /**
     * Returns a boolean indicating whether the plugin has it's own control
     * panel section.
     *
     * @return bool
     */
    public function hasCpSection()
    {
        return false;
    }

    /**
     * Adds all categories to the sitemap.
     */
    public function renderSitemap()
    {
        $groups = craft()-&gt;categories-&gt;getAllGroups();

        foreach ($groups as $group) {
            craft()-&gt;sitemap-&gt;addCategoryGroup($group);
        }
    }
}