Minimize Navigation Output

As referenced in an earlier post, "CSS Control Adapters for ASP.NET", content management systems often wrap several inline tags and identifiers around theme plugins and core system functions. Code output of this manner can hinder SEO and create headaches for style management. To minimize the unwanted tags and improve front end maintenance in a CMS such as Wordpress, only a few lines of code are required in the functions.php file to offset your site navigation output.

<?php

// clean string
function sanitize($v) { 
    $v = preg_replace('/[^a-zA-Z0-9s]/', '', $v);
    $v = str_replace(' ', '-', $v);
    $v = strtolower($v);
    return $v;
}


// find associated class
function nav_class_filter($var) {
    return is_array($var) ? array_intersect($var, array('current-menu-item')) : '';
}
add_filter('nav_menu_css_class', 'nav_class_filter', 100, 1);


// find associated tag ID
function nav_id_filter($id, $item) { 
    return 'nav-'.sanitize($item->title);
}
add_filter( 'nav_menu_item_id', 'nav_id_filter', 10, 2 );

?>