Special character mangling in WordPress and SyntaxHighlighter

The SyntaxHighlighter WordPress plug-in is used to display code ‘as is’; unfortunately WordPress again is too clever for its own good and mangles special characters like & and ” even before the text gets to the SyntaxHighlighter blocks in the Block Editor.

The solution is to add a filter function in your custom functions.php file:

function my_syntaxhighlighter_precode( $code, $atts, $tag ) {
	if ( 'code' === $tag ) {
		$code = wp_specialchars_decode( $code, ENT_QUOTES );
	}
	return $code;
}
add_filter( 'syntaxhighlighter_precode', 'my_syntaxhighlighter_precode', 10, 3 );

Source links:
https://developer.wordpress.org/reference/functions/wp_specialchars_decode/
https://developer.wordpress.org/reference/functions/wp_specialchars/
https://stackoverflow.com/questions/43291221/syntaxhighlighter-evolved-3-2-1-plugin-converted-to-amplt
https://wordpress.org/support/topic/ampersand-character/

Leave a Reply

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