LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / PHP Codes » Getting Code Executed Inside PRE/CODE Tags Even They Are Properly Escaped

Getting Code Executed Inside PRE/CODE Tags Even They Are Properly Escaped

Getting-Code-Executed-Inside-PRE-CODE-Tags-Even-They-Are-Properly-Escaped
Getting code executed inside pre/code tags even if they are properly escaped in WordPress blog or custom website then here is a quick fix for WordPress and custom websites.

It’s time to fix this up to stop breaking your templates or blog pages until you find the main culprit because sometimes this happens out of the box you are doing perfectly but this started to happen. There is a custom PHP code also shared below for your custom website a special code for WordPress Blogs also.

There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Features:

  1. Light Weight.
  2. Pure PHP Code.
  3. Cross Browser.
  4. Fully Customizable.

Custom Website: Getting Code Executed Inside PRE/CODE Tags Even They Are Properly Escaped?

There are a few easy and understandable steps to achieve your desired functionality that we are gonna share below.

<?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();
}   
?>

<?php echo escapePreformattedCode($YOUR_CONTENT_VARIABLES); ?>

WordPress: Getting Code Executed Inside PRE/CODE Tags Even They Are Properly Escaped?

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
  );
}

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need.

Recommended For You:
JavaScript Based AutoComplete DropDown Using Pure Vanilla JavaScript

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *