Change HTML symbols (< >) to special symbols when commenting

Recently wrote about how to insert code into the text of an article. Today, I want to share a convenient method for inserting code into the body of a comment.

Task: in comments, find the <pre> and <code> tags and replace all < and > symbols within them with their corresponding special characters: &lt; and &gt;.

It is important to determine when to make the replacement. For example, some plugins make the replacement during page generation, i.e. the transformation function is applied to each comment every time the page is opened and the modified comment is displayed on the screen. I haven't seen a plugin yet that would replace before writing the text to the database, so that the text could be obtained without transformations. I think this is the best approach. In the worst case, replacement can always be reversed, where necessary...

Task solution (new code version):

add_filter('pre_comment_content', 'kama_convert_html');
/*
 * Converts html symbols: < > inside tags pre, code to special symbols < > in the text
 * v1.0
 */
function kama_convert_html_entities( $text, $convert_in='pre|code|var'){
	return preg_replace_callback ('!<('. $convert_in .')([^>]*)>(.*?)</\\1>!ims', '__kama_convert_html_entities_cb', $text);
}
function __kama_convert_html_entities_cb( $matches ){
	$out = str_replace( array('<','>'), array('<','>'), $matches[3] );
	$out = preg_replace( "~((?<=\n|^|\t))[ ]{4}~m", "\\1\t", $out ); // replace 4 spaces with tabs

	return "<$matches[1]$matches[2]>$out</$matches[1]>";
}

Place the function in the functions.php file of your theme.

What does this function do?

Upon publication of a comment, the function finds text within the <pre> and/or <code> tags and replaces < and > symbols in the found text with &lt; and &gt;.

The replacement occurs before the comment text is written to the database. If replacement is needed right before the text is displayed on the screen, replace the line:

add_filter('pre_comment_content', 'kama_convert_html');
// with
add_filter('comment_text', 'kama_convert_html');

 

How to check that everything is working?

After installing the function, try leaving a comment with the following text:

Checking character conversion in comments. <div></div>
<code>Here will be code <div> </code>
First intermediate text <span></span>
<pre>Here will be code 2 <span> </pre>
Intermediate text <strong></strong>
<code>Here will be some more code <div> </code>
Some more simple text <b></b>
<pre>Here will be code <span> </pre>
Finally, some more text <div></div>

As a result, the following text should be written to the database:

Checking character conversion in comments. <div></div>
<code>Here will be code &lt;div&gt; </code>
First intermediate text <span></span>
<pre>Here will be code 2 &lt;span&gt; </pre>
Intermediate text <strong></strong>
<code>Here will be some more code &lt;div&gt; </code>
Some more simple text <b></b>
<pre>Here will be code &lt;span&gt; </pre>
Finally, some more text <div></div>

Preserving any code in the text of an article (post)

The same trick can be done with the content of the article. To do this, add another filter to the function:

add_filter('pre_comment_content', 'kama_convert_html');
// When updating a post in the admin panel
add_filter('content_save_pre', 'kama_convert_html');

Old version

The old (first) version of the code works the same, only the code is longer:

/* Convert html characters < > into special characters when sending a comment */
function kama_html_replace_char_pre( $matches ){
	$out = str_replace (array ('<'   ,'>'),   array ('&lt;','&gt;'),  $matches[2] );
	return "<pre{$matches[1]}>".$out."</pre>";
}
function kama_html_replace_char_code ($matches){
	$out = str_replace (array ('<'   ,'>'),   array ('&lt;','&gt;'),  $matches[2] );
	return "<code{$matches[1]}>".$out."</code>";
}
function kama_convert_html ($comment_text){ // prepares the commentator's content
	$comment_text = preg_replace_callback ('!<pre([^>]*)>(?:\r\n|\n|\r|)(.*?)(?:\r\n|\n|\r|)</pre>!ims', 'kama_html_replace_char_pre', $comment_text);
	$comment_text = preg_replace_callback ('!<code([^>]*)>(?:\r\n|\n|\r|)(.*?)(?:\r\n|\n|\r|)</code>!ims', 'kama_html_replace_char_code', $comment_text);
	return $comment_text;
}
add_filter ('pre_comment_content','kama_convert_html');