The XML Sitemap module is a great way to generate a sitemap that can be automatically submitted to search engines. The admin interface allows to determine the default inclusion behavior for content types, taxonomy, menus, etc. On a few projects, I've had the need to programmatically remove certain nodes and taxonomy term landing pages dynamically from the generated XML Sitemp. In this quick code snippet I'll show how I was able to remove these items.
I got started by reviewing the xmlsitemap.api.php file included in the module. The function hook_xmlsitemap_link_alter(&$link) looked promising because it accepts the link as an argument, passed by reference, which would allow you to make modifications to it.
I implemented this hook in my module...
<?php
function MYMODULE_xmlsitemap_link_alter(&$link) {
// define a variable to determine if the link should be removed
// NOTE: when the xml sitemap is generated, every link will be passed through this hook
$remove_link = FALSE;
// check for taxonomy term links
if ($link['type'] == 'taxonomy_term') {
// define a list of taxonomy term ids to exclude
// NOTE: in my actual projects, this code was more dynamic :)
$excluded_terms = array(1,2,3,4,5);
// check if the link should be excluded
if (in_array($link['id'], $excluded_terms)) {
$remove_link = TRUE;
}
}
// check for node links
elseif ($link['type'] == 'node') {
// define a list of node ids to exclude
$excluded_nodes = array(1,2,3,4,5);
// check if the link should be excluded
if (in_array($link['id'], $excluded_nodes)) {
$remove_link = TRUE;
}
}
// remove link as necessary
// NOTE: if the link has been flagged to be excluded,
// setting the $link data to an empty array should remove it from the xml sitemap
if ($remove_link) {
$link = array();
}
}
?>Now when my XML sitemap is generated, each link will be passed through this hook. If the link meets my conditional logic, it will be flagged to be removed and will be excluded from the xml output. This code works well for situations where you do not want to permanently exclude links from your sitemap.









