We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3467
    • 10 Posts
    I find the Google sitemap thing very useful, specially for new sites and I only found an addon for MODx that uses friendly urls. So I wrote a plugin that creates a physical sitemap.xml file. The plugin acts on events publish, unpublish, delete and save. On Undelete would be nice to, but I didn’t find an event for that?

    The plugin doesn’t dynamically yet use the tags
    changefreq
    and
    priority
    , but you can change them for every page in the code for the plugin. Have been playing with some TVs to be able to alter changefreq and priority for every page, but thats not working good enough for the moment.

    And, you’ll need write permission to the root of your website, at least to the file sitemap.xml.

    Well, just create the plugin add the code below, and connect it to mentioned events:
    
    function create()
    {
    	global $modx;
    	$File =$modx->config['base_path'].'sitemap.xml';
      
            //change this if you want
            $changefreq = 'weekly';
            $priority = '0.5';
    
    	$Handle = fopen($File, 'w');
    	        
    	$result = $modx->db->select("*", $modx->getFullTableName('site_content') ,"deleted=0 AND published=1");
    	$total_rows = $modx->db->getRecordCount( $result );
    	$xml_output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    	$xml_output .= "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";
    	date_default_timezone_set("Europe/London");
    
    	if( $total_rows >= 1 ) 
    	{
    		while( $row = $modx->db->getRow( $result ) ) 
    		{
    			$xml_output .= "<url>\n";
    			$xml_output .= "\t<loc>" . $modx->config['site_url'] . "index.php?id=" . $row['id'] ."</loc>\n";
    			$date = date("Y-m-d", $row['editedon']);
    		        $xml_output .= "\t\t<lastmod>" . $date . "</lastmod>\n";
    		        $xml_output .= "\t\t<changefreq>" . $changefreq . "</changefreq >\n";
    		        $xml_output .= "\t\t<priority>" . $priority . "</priority>\n";
    			$xml_output .= "</url>\n";	
    		}
    	}
    		
    	$xml_output .= "</urlset>"; 
    	fwrite($Handle, $xml_output);
    	fclose($Handle);
    
    }
    
    
    $e = &$modx->Event; 
    switch ($e->name) 
    { 
    	case "OnDocPublished":
    		create();	     
    		break;
    	case "OnDocUnPublished":
    		create();
    		break;
    	case "OnDocFormSave": 
    		create();
    		break;
    	case "OnDocFormDelete": 
    		create();
    	break;
    
     }