public function the_content($content) { if (!$content || !is_singular()) { return $content; } $dom = new DOMDocument(); $dom->loadHTML($content); $xpath = new DOMXPath($dom); $body = $xpath->query('/html/body')->item(0); $headings = $xpath->union_query(['h1', 'h2', 'h3', 'h4', 'h5', 'h6'], $body, true); // Set IDs. foreach ($headings as $heading) { // @todo May want to consider a max-length for IDs. $id = $this->get_id($heading); $id = $this->maybe_make_unique_id($id); $heading->setAttribute('id', $id); $this->headings[$id] = ['tag' => $heading->tagName, 'value' => trim($heading->nodeValue)]; $this->append_section_link($heading, $id); } return $dom->saveHTML(); }