Prevent wptexturize incorrectly encoding characters in a script

In WordPress wp-includes/formatting.php, just after the line:

	$textarr = preg_split( $regex, $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );

I changed the PHP to be:

	$inScript = false;
	foreach ( $textarr as &$curl ) {
		// Only call _wptexturize_pushpop_element if $curl is a delimiter.
		$first = $curl[0];
		if ($inScript && $first !== '<') {
			// Avoid texturizing any part of a script
			continue;
		} elseif ( '<' === $first ) {
			if ( '<!--' === substr( $curl, 0, 4 ) ) {
				// This is an HTML comment delimiter.
				continue;
			} elseif ( '<script>' === substr( $curl, 0, 8) ) {
				// This is a script
				$inScript = true;
			} elseif ( '</script>' === substr( $curl, 0, 9 ) ) {
				$inScript = false;
			} elseif ( $inScript ) {
				// It is in a script and the current $curl does not end the script.
				continue;
			}

This change prevents WordPress from incorrectly escaping some characters when I embed some freestyle JavaScript in posts.

Leave a Reply

Your email address will not be published.

Color scheme: