Important
This plugin is no longer maintained.
We recommend the SEOmatic plugin or the SEO plugin instead.
A simple plugin for Craft that generates a sitemap.xml based on enabled sections.
- Copy the
sitemap/folder intocraft/plugins/ - Go to Settings → Plugins and click the “Install” button next to “Sitemap”
Within the plugin settings, check the boxes in the “Enabled” column to include them in the sitemap.
To view the output visit /sitemap.xml.
This plugin exposes various service methods, which can be used to add custom items to the sitemap through the renderSitemap hook. Please read the official ‘Hooks and Events’ documentation, if you’re not sure how this works.
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:
publicfunctionrenderSitemap()
{
// Get an ElementCriteriaModel from the ElementsService$criteria = craft()->elements->getCriteria(ElementType::Entry);
// Specify that we want entries within the ‘locations’ section$criteria->section = 'locations';
// Loop through any entries that were foundforeach ($criteria->find() as$locationEntry)
{
// Here we’re building a path using the entry slug.// This might match a custom route you’ve defined that// should be included in the sitemap.$path = 'cars-for-sale-in-' . $locationEntry->slug;
// Make sure that we’re using a full URL, not just the path.$url = UrlHelper::getSiteUrl($path);
// For the sake of this example, we’re setting the $lastmod// value to the most recent time the location entry was// updated. You can pass any time using the DateTime class.$lastmod = $locationEntry->dateUpdated;
// Add the URL to the sitemapcraft()->sitemap->addUrl($url, $lastmod, Sitemap_ChangeFrequency::Daily, 0.5);
}
}And here’s an example of the resulting element in the sitemap XML:
<url>
<loc>http://example.com/cars-for-sale-in-scotland</loc>
<lastmod>2015-08-28T15:08:28+00:00</lastmod>
</url>There’s several service methods made available to add items to the sitemap.
Adds a URL to the sitemap.
$loc = UrlHelper::getSiteUrl('special/route');
$lastmod = newDateTime('now');
craft()->sitemap->addUrl($loc, $lastmod, Sitemap_ChangeFrequency::Yearly, 0.1);Adds an element to the sitemap.
$element = craft()->elements->getElementById(2);
craft()->sitemap->addElement($element, Sitemap_ChangeFrequency::Daily, 1.0);Adds all entries in the section to the sitemap.
$section = craft()->sections->getSectionByHandle('homepage');
craft()->sitemap->addSection($section, Sitemap_ChangeFrequency::Weekly, 1.0);Adds all categories in the group to the sitemap.
$group = craft()->categories->getGroupByHandle('news');
craft()->sitemap->addCategoryGroup($group);Gets a element URL for the specified locale. The locale must be enabled.
echo$element->url;
// http://example.com/en/hello-worldechocraft()->sitemap->getElementUrlForLocale($element, 'fr');
// http://example.com/fr/bonjour-mondeGets a URL for the specified locale. The locale must be enabled.
echo UrlHelper::getSiteUrl('foo/bar');
// http://example.com/en/foo/barechocraft()->sitemap->getUrlForLocale('foo/bar', 'fr');
// http://example.com/fr/foo/barEnumeration of valid changefreq values.
Sitemap_ChangeFrequency::Always
Sitemap_ChangeFrequency::Hourly
Sitemap_ChangeFrequency::Daily
Sitemap_ChangeFrequency::Weekly
Sitemap_ChangeFrequency::Monthly
Sitemap_ChangeFrequency::Yearly
Sitemap_ChangeFrequency::Never