With CMSMS we have a nice number of modules that help us with Automation of some tasks and make our webmaster life easier.
So today we will have a look at SiteMapMadeSimple module written by Robert Campbell. This module helps us generating a Sitemap .xml file for Searchengines.
Downside of the module is, that it only detects changes concerning regular CMS content and pages, but in some cases you would probably also wish to update your Sitemap when writing new News article or Blog entry or whatever you would like to have indexed, for example Products module pages.
As first we have to setup SiteMapMadeSimple module and template that will include our News articles. You can download the module directly from Forge or use ModuleManager in CMSMS Backend to install it.
Once the module is installed you will find it in "Extensions » SiteMapMadeSimple", click on a menu to get into module interface and start with Template.
Now create a Sitemap Template that will look something like that.
{foreach from=$output item='page'}
<url>
<loc>{$page->url}</loc>
<lastmod>{$page->date|date_format:"%Y-%m-%d"}</lastmod>
<priority>{$page->priority}</priority>
<changefreq>{$page->frequency}</changefreq></url>
{/foreach}
{capture assign='junk'}
{cms_module module='News' detailpage='your-news-detail-page' number='1000'}
{/capture}
{foreach from=$items item='entry'}
<url>
<loc>{$entry->moreurl}</loc>
<lastmod>{$entry->postdate|date_format:"%Y-%m-%d"}</lastmod>
<priority>{$page->priority}</priority>
<changefreq>{$page->frequency}</changefreq>
</url>
{/foreach}
</urlset> Hit Submit and save your Template. Take a note that detailpage= parameter was used. The reason for that is that if you don't specify a detailpage you might get in trouble of multiple URL's for one article (you might have noticed that detail article page contains $content_id number). This means i would recommend you, to use detailpage parameter for Frontend aswell as Sitemap templates.
Now as our Sitemap template is ready we are going to create a UDT so Sitemap file is updated when adding or deleting News articles.
To do so go to "Extensions » User Defined Tags" and create new UDT, name it however you want, i named it "generate_sitemap".
Now add following chunk of code to that UDT.
$gCms = cmsms();
$config = $gCms->GetConfig();
$sitemap = cms_utils::get_module('SiteMapMadeSimple');
$template = $sitemap->GetPreference('dflt_xml');
if( isset($params['template'] ) )
{
$template = trim($params['template']);
}
$template = 'xml_'.$template;
$xmlfile = $sitemap->ProcessTemplateFromDatabase($template);
$xmlfile = $sitemap->GenerateSiteMap($params);
if( !$sitemap->GetPreference('static_sitemap') )
{
$handlers = ob_list_handlers();
for ($cnt = 0; $cnt < sizeof($handlers); $cnt++) { ob_end_clean(); }
header("Content-Type: application/xml");
echo $xmlfile;
exit;
}
// Generating a static sitemap
$fn = cms_join_path($config['root_path'],'sitemap.xml');
$f = @fopen($fn,'w');
@fwrite($f,$xmlfile);
@fclose($f); Now we have our UDT ready and all we have to do is make use of CMSMS built in "Event Manager" to update our sitemap.xml file.
To do that go to "Extensions » Event Manager", you will find a "dropdown" there, select "News" from that dropdown and hit Submit.
You will see a list of Events related to News module.
Click on NewsArticleAdded event and select from the dropdown your newly created UDT, in my case that was "generate_sitemap" and click on "Add".
Repeat this step for NewsArticleDeleted and NewsArticleEdited.
Thats it, from now on your sitemap.xml will be updated on each of selected events.
Hi Thanks for this post I tried to do the same with CompanyDirectory and CGCalendar int the same SiteMap Made Simple template with no success.
Hi Goran,
I did notice some issues related to the usage of the Event Manager. (I tested this for CGBlog, not for News)
If a user presses apply for the first time while editing a CGBlog article, the "actionid" and the "actionparams" change. The post date changes and the category selection is forgotten.
If a user presses apply for the second time, the error message "No Title Given" is shown.
You can verify this by adding "{get_template_vars}ACTIONPARAMS:{$actionparams|@debug_print_var}" at the end of your /modules/CGBlog/templates/editarticle.tpl file
I found that removing the "generate_sitemap" Event Handler for the CGBlogArticleEdited event fixes the issue. BUT if a user changes the title of an article and the URL is automatically changed accordingly, the sitemap isn't updated with the new info. The old URL will however still redirect visitors to the correct article.
Concretely, the "capture assign='junk'" of the "cms_module module='CGBlog'" is causing the issue but as this is an essential part of the solution, i don't think there is a different approach...
Greetings, Manuel
Thx for sharing this Goran! I've just used your solution for the first time on a new website that uses CGBlog and it's working perfectly!
Greetings, Manuel
Hi G,
yes, basically it will work with any module that has detailpages and triggers an event on submit,edit...
But you should look at that module to find out correct variables in the template for example difference between News and CGBlog would be in detail url where News is using {$entry->moreurl} it would be in CGBlog {$entry->detail_url}
Hello,
Nice tip! If I change the references to News into CGBlog, will this tip also work?
Thanks in advance for your reply.
Thanks Goran. I hadn't realized that News, etc didn't update the site map. This UDT will be very helpful.