Skip to content

Instantly share code, notes, and snippets.

@tarikcayir
Created January 25, 2014 20:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarikcayir/8622794 to your computer and use it in GitHub Desktop.
Save tarikcayir/8622794 to your computer and use it in GitHub Desktop.
<?php
/**
* Content heading auto ID
*
* Examples;
* <h*>Test Header</h*>
* <h* id="test-header">Test Header</h*>
*/
add_filter( 'the_content', 'content_heading_filter' );
function content_heading_filter( $content ) {
return $content = preg_replace_callback("#<(h[1-6])>(.*?)</\\1>#", "re_title", $content);
}
function re_title($match) {
list($_unused, $h2, $title) = $match;
$id = sanitize_title($title);
return "<$h2 id='$id'>$title</$h2>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment