0

Note: I am using https://wordpress.org/plugins/classic-editor/ with latest updated WordPress.

I have a Blog at https://www.exeideas.com/ and I share code snippet here also that was working before perfectly as I share the Escaped Codes inside <pre> and <code> tag but suddenly it started executing codes inside the <pre> and <code> tags all over my blog for example at https://www.exeideas.com/2022/10/responsive-collapsible-sidebar-and-tab-nav.html

enter image description here

It was working till yesterday. Any way to fix this all over my blog where I have about 3800+ articles that contain <pre> and <code> tags, I tried the following solutions but was not successful...

functions.php

add_filter( 'the_content', 'pre_content_filter', 0 );
function pre_content_filter( $content ) {
    return preg_replace_callback( '|<pre.*>(.*)</pre|isU' , 'convert_pre_entities', $content );
}
function convert_pre_entities( $matches ) {
    return str_replace( $matches[1], html_entity_decode( $matches[1] ), $matches[0] );
}

functions.php

add_filter('the_content','pre_esc_html',9);
function pre_esc_html($content) {
  return preg_replace_callback(
    '#(<pre.*?>)(.*?)(</pre>)#imsu',
    create_function(
      '$i',
      'return $i[1].esc_html($i[2]).$i[3];'
    ),
    $content
  );
}

single.php

<?php
function escapeRecursively($node) {
    if ($node instanceof DOMText)
        return $node->textContent;

    $children = $node->childNodes;
    $content = "<$node->nodeName>";
    for ($i = 0; $i < $children->length; $i += 1) {
        $child = $children->item($i);
        $content .= escapeRecursively($child);
    }

    return "$content</$node->nodeName>";
}
function escapePreformattedCode($html) {
    $doc = new DOMDocument();
    $doc->loadHTML($html);

    $pres = $doc->getElementsByTagName('pre');
    for ($i = 0; $i < $pres->length; $i += 1) {
        $node = $pres->item($i);

        $children = $node->childNodes;
        $content = '';
        for ($j = 0; $j < $children->length; $j += 1) {
            $child = $children->item($j);
            $content .= escapeRecursively($child);
        }
        $node->nodeValue = htmlspecialchars($content);
    }

    return $doc->saveHTML();
}   
?>
<div class="post">  
<?php echo escapePreformattedCode(the_content()); ?>
<div class="clear"></div>
</div>

But none of this is working. This happen to all of my old posts too that were working yesterday. What were the fix to it and avoid editing all 3800+ posts...

3
  • Should it not be <pre><code> instead of <pre>? Things don't suddenly change out of nowhere, something was modified/changed/updated, what has changed since yesterday?
    – Tom J Nowell
    Commented Oct 30, 2022 at 12:56
  • No. I didn't do anything yesterday. Just post an article today and when previewed it then got this error. Also, my all previous posts got this error automatically. My automatic updates are on so I tried to roll back an update to try but still got this error. Commented Oct 30, 2022 at 13:08
  • I just figured out some quick fixes to make my blog perfect for viewers but still confused about why this is happening. In DB, my post content is encoded properly so I think there is a problem with <?php the_content(); ?> in my theme Commented Oct 30, 2022 at 13:27

1 Answer 1

0

Quick Fix Answer:


Add the below code to your theme functions.php file for a quick fix. Rest still trying to find the reason for this error so if you have any then welcome to comment here so I will mark your answer as accepted.

/* ------------------------------------------------------------------------- *
 *  Stop Executing Codes Inside Pre/Code Tags
/* ------------------------------------------------------------------------- */
add_filter('the_content','pre_esc_html',1);
function pre_esc_html($content) {
    return preg_replace_callback(
    '#(<pre.*?>)(.*?)(</pre>)#imsu',
    create_function(
      '$i',
      'return $i[1].htmlspecialchars($i[2]).$i[3];'
    ),
    $content
  );
}

add_filter('the_content','code_esc_html',1);
function code_esc_html($content) {
    return preg_replace_callback(
    '#(<code.*?>)(.*?)(</code>)#imsu',
    create_function(
      '$i',
      'return $i[1].htmlspecialchars($i[2]).$i[3];'
    ),
    $content
  );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.