In WordPress, to escape HTML tags inside a <pre> or <code> tag, you can use the esc_html() function. This function will convert any special characters in the HTML to their corresponding HTML entities, which will prevent them from being interpreted as actual HTML tags. Here is an example of how to use it:

<?php
$content = '<p>This is some <strong>sample</strong> text.</p>';
echo '<pre>' . esc_html( $content ) . '</pre>';
?>

This code assigns a string containing some HTML to the variable $content and then wraps it in a <pre> tag and uses the esc_html() function to escape any special characters. When this code is executed, the output will be:

<p>This is some <strong>sample</strong> text.</p>

Alternatively, you can use the wp_kses() function which allows you to specify which HTML tags and attributes are allowed, and will remove any that are not on the list.

<?php
$content = '<p>This is some <strong>sample</strong> text.</p>';
$allowed_tags = array(
    'p' => array(),
    'strong' => array()
);
echo '<pre>' . wp_kses( $content, $allowed_tags ) . '</pre>';
?>

This code will output the same as the above example.

It is important to note that, esc_html() function is a good way to escape data that will be displayed as plain text, but it should not be used for data that will be displayed as HTML. In such cases, you should use wp_kses() function or other similar function.

(Visited 58 times, 1 visits today)
Was this article helpful?
YesNo
Close Search Window