Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
* 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