<p>You'll need to create a separate plugin, which adds the categories to the sitemap in response to the <code>renderSitemap</code> hook.</p>

<p>Here's what <a href="https://craftcms.com/docs/plugins/setting-things-up#your-primary-plugin-class" rel="noreferrer">your main plugin file</a> 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 <a href="https://craftcms.com/docs/plugins/introduction" rel="noreferrer">the official documentation</a>.</p>

<pre><code>&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);
        }
    }
}
</code></pre>

CC BY-SA 3.0
2017-05-26T15:40:11.017Z
Whatever it takes to ensure Brad doesn't get that bounty.
Stephen Lewis
CC BY-SA 3.0
2017-05-26T10:55:58.097Z
2017-05-26T10:55:58.097Z
Stephen Lewis
<p>I'm trying to setup a sitemap using the <a href="https://github.com/joshuabaker/craft-sitemap" rel="nofollow noreferrer">craft-sitemap plugin</a>, however it only covers sections by default and I need to add category groups into it, as half of the URLs are in categories.</p>

<p>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."</p>

<p><strong>Hooks</strong>
<code>renderSitemap</code></p>

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

<p>Here’s an example plugin hook method with comments:</p>

<pre><code>addCategoryGroup(CategoryGroupModel $categoryGroup, [$changefreq, [$priority]])
</code></pre>

<p>Adds all categories in the group to the sitemap.</p>

<pre><code>$group = craft()-&gt;categories-&gt;getGroupByHandle('news');
craft()-&gt;sitemap-&gt;addCategoryGroup($group);
</code></pre>

<p>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.</p>

<p>I would appreciate some advice on this please?</p>

CC BY-SA 3.0
2017-05-27T20:41:32.390Z
Just an FYI that the PutYourLightsOn Sitemap plugin handles category groups, Craft Commerce product types and custom URL's out of the box, although it is a commercial plugin: 
https://www.putyourlightson.net/craft-sitemap
Ben Croker
<plugins><plugin-sitemap>
add categories to sitemap plugin through the renderSitemap hook
# add categories to sitemap plugin through the renderSitemap hook

(Asked by: username_0 on 2017-05-23)

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()->categories->getGroupByHandle('news');
craft()->sitemap->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?

**Tags:** plugins, plugin-sitemap
> **Comments:**
> **username_1**: Just an FYI that the PutYourLightsOn Sitemap plugin handles category groups, Craft Commerce product types and custom URL's out of the box, although it is a commercial plugin:  https://www.putyourlightson.net/craft-sitemap  

---

## 1 answers

### Answer by username_2 on 2017-05-26. Score: 4 | Accepted answer

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.
categories->getAllGroups();

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

> **Comments:**
> **username_2**: Whatever it takes to ensure Brad doesn't get that bounty.  

---

