// Add anchors to headings and create a nav
function auto_id_headings( $content ) {
$headings = array();
$content = preg_replace_callback( '/(\<h[1-6](.*?))\>(.*)(<\/h[1-6]>)/i', function( $matches ) use (&$headings) {
if ( ! stripos( $matches[0], 'id=' ) ) :
$matches[0] = $matches[1] . $matches[2] . ' id="' . [sanitize_title](http://codex.wordpress.org/Plugin_API/Filter_Reference/sanitize_title)( $matches[3] ) . '">' . $matches[3] . $matches[4];
$headings[] = array([sanitize_title](http://codex.wordpress.org/Plugin_API/Filter_Reference/sanitize_title)($matches[3]), $matches[3]);
endif;
return $matches[0];
}, $content );
if(count($headings)) {
$tabs = '<nav id="navbar-review" class="navbar navbar-light bg-light sticky-top"><div class="container"><ul class="nav nav-pills" role="tablist">';
foreach($headings as $heading) {
$tabs .= '<li class="nav-item"><a class="nav-link" href="#'.$heading[0].'">'.$heading[1].'</a></li>';
}
$tabs .= '</ul></div></nav>';
$content = $tabs.$content;
}
return $content;
}
[add_filter](http://codex.wordpress.org/Function_Reference/add_filter)( '[the_content](http://codex.wordpress.org/Plugin_API/Filter_Reference/the_content)', 'auto_id_headings' );