Update your sitemap.xml with a UDT

Sat. 26. November 2011 CMSMS UDTs Modules Goran Ilic3 comments
Update your sitemap.xml with a UDT

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.

Install Module from ModuleManager

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.

Update Event Manager News options

Thats it, from now on your sitemap.xml will be updated on each of selected events.



Article was created by Goran Ilic

Goran Ilic

Goran Ilic is a designer, allrounder, husband, father, and founder of I do this blog.
After long search for a perfect tool for his clients he has found CMSMS and devoted his passion to it.

Visit Goran Ilic's Website


  • Twitter
  • Facebook
  • LinkedIn
  • LinkedIn
« Back to Articles

Comments


Leave a Comment

 Add a Review of this item 

Categories

Visit GeekMoot 2012

Archive

From Twitter

Designed and developed by idea arts. I do this! © Copyright 2010-2012

Go back to Top