Skip to content

Instantly share code, notes, and snippets.

@yusufhm
Last active January 30, 2018 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yusufhm/85048b96127026d3b341 to your computer and use it in GitHub Desktop.
Save yusufhm/85048b96127026d3b341 to your computer and use it in GitHub Desktop.
Add links programmatically to xmlsitemap
<?php
/**
* Implements hook_cron().
*/
function MYMODULE_cron() {
MYMODULE_xmlsitemap_links();
}
/**
* Implements hook_xmlsitemap_links
*/
function MYMODULE_xmlsitemap_links() {
$menu_links = menu_load_links("main-menu");
$sorted = array();
foreach ($menu_links as $menu_link) {
if ($menu_link['link_path'] == '<view>' || $menu_link['hidden'] == 1) { continue; }
$absolute_weight = $menu_link['weight'];
if ($absolute_weight < 0) { $absolute_weight += 100; }
if ($menu_link['depth'] > 1) { $absolute_weight = (($menu_link['depth'] - 1) * 10) + $absolute_weight; }
$sorted[$absolute_weight] = array(
'type' => 'MYMODULE',
'id' => $menu_link['mlid'],
'loc' => $menu_link['link_path'],
'lastmod' => time(),
'changefreq' => 4600,
'priority' => 0.5,
);
}
ksort($sorted);
$links = array_values($sorted);
foreach ($links as $link) {
xmlsitemap_link_save($link);
}
}
@ArnoldMessifet
Copy link

Great !!! Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment