Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function replace_content_div_with_main() {
- ob_start(function($buffer) {
- $dom = new DOMDocument();
- $dom->loadHTML(mb_convert_encoding($buffer, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
- $xpath = new DOMXPath($dom);
- $divs = $xpath->query('//div[contains(@class, "wp-site-blocks")]');
- if ($divs->length > 0) {
- foreach ($divs as $div) {
- $containsHeader = $xpath->query('.//header', $div)->length > 0;
- $containsFooter = $xpath->query('.//footer', $div)->length > 0;
- if (!$containsHeader && !$containsFooter) {
- $main = $dom->createElement('main');
- $main->setAttribute('class', $div->getAttribute('class'));
- while ($div->firstChild) {
- $main->appendChild($div->firstChild);
- }
- $div->parentNode->replaceChild($main, $div);
- break;
- }
- }
- }
- $buffer = $dom->saveHTML();
- return $buffer;
- });
- }
- add_action('wp_loaded', 'replace_content_div_with_main');
- function end_output_buffer() {
- if (ob_get_length()) {
- ob_end_flush();
- }
- }
- add_action('shutdown', 'end_output_buffer', 999);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement