add_filter('the_content', 'add_id_attribute_to_headings');
function add_id_attribute_to_headings($content): string {
return preg_replace_callback('#<(h[1-6])(.*?)>(.*?)</(h[1-6])>#', 'add_id_attribute_to_heading_callback', $content);
function add_id_attribute_to_heading_callback($match): string {
[$full, $el_start, $attributes, $content, $el_end] = $match;
if (str_contains($full, ' id="')) {
$id = sanitize_title_with_dashes($content);
return '<{$el_start} id="{$id}" {$attributes}>{$content}</{$el_end}>';