(.*?)<\/\1>/is', function ( $matches ) use ( &$used_ids, $permalink ) { $tag = $matches[1]; $attrs = $matches[2]; $text = wp_strip_all_tags( $matches[3] ); // Skip if class contains sp-smart-post-title. if ( preg_match( '/class=["\'][^"\']*sp-smart-post-title[^"\']*["\']/', $attrs ) ) { return $matches[0]; } // Skip if ID already exists. if ( preg_match( '/id=["\'].*?["\']/', $attrs ) ) { return $matches[0]; } $id = sanitize_title( $text ); while ( in_array( $id, $used_ids, true ) ) { $id .= '-' . wp_rand( 10, 99 ); } $used_ids[] = $id; return sprintf( '<%1$s%2$s>%4$s#', $tag, $attrs, esc_attr( $id ), $matches[3], esc_url( $permalink ) ); }, $content ); } /** * Add id to heading function * * @param string $content Post content. * @param string $permalink Post permalink. * @return string $content HTML markup. */ private function add_ids_to_headings( $content, $permalink ) { if ( empty( $content ) ) { return $content; } return $this->add_ids_to_headings_fallback( $content, $permalink ); } /** * Check table of content function * * @param string $content Post content. * @return boolean */ private function has_toc_block( $content ) { return preg_match( '/