Hacking WordPress (covers 2.0)

There is an issue related to how WordPress displays PREformatted html.
By default WP translates any post retrieved from the database before displaying it. It uses two filter functions: wptexturize and wpautop, both added to the same filter: the_content. It also translates any other piece of content (Title, Excerpt, …), but I don’t care now.

The function wptexturize is harmless but a bit annoying.
The concept behind WP is keep it simple, isn’t it? If I post a ” I expect a ” to be displayed, not a “ or a ”. Authors always need to see what they mean!

Until an optional setting appears for turning it off, I recommend you to turn it off now.
It’s quite easy. Just edit the file wordpress/wp-includes/default-filters.php and comment out the line that reads

add_filter('the_content', 'wptexturize');

The function wpautop is harmful, so here is a replacement.

Step 1/2
Edit the file wordpress/wp-includes/functions-formatting.php and copy and paste this function after the function wpautop.

function ae_autop( $unprocessed, $br = 1 ) {
	$processed = "<br />n";
	while( strlen( $unprocessed ) > 0 ) {
		$debugging += 1;
		preg_match( '{^([^<]*)?(</?[^>]+?>)?(.*)$}s', $unprocessed, $matches );
		$text = $matches[1]; // as much text as possible before an HTML tag
		$text = str_replace( "n", "<br />n", $text );
		$tag = $matches[2]; // an HTML tag
		$unprocessed = $matches[3]; // the rest of $unprocessed
		if( preg_match( '{<preb}i', $tag ) ) {
			preg_match( '{^(<preb.*?</pre>)?(.*)$}is', $tag . $unprocessed, $skipping );
			$tag = $skipping[1]; // a pre block
			$unprocessed = $skipping[2]; // the rest of $unprocessed
		}
		$processed .= $text . $tag;     
	}
	return $processed; 
}

Step 2/2
Last but not least, edit the file wordpress/wp-includes/default-filters.php and comment out the line that reads

add_filter('the_content', 'wpautop');

After it, add this line

add_filter('the_content', 'ae_autop');

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.