WP-Mix

A fresh mix of code snippets and tutorials

Filter pre tags in bbPress

A couple of functions used at htaccessbook.com to enable unescaped/unencoded pre and code tags in bbPress forum posts.

function bbp_replace_brackets($content) {
	$filter_pre_content  = preg_replace_callback('#\<pre\>(.+?)\<\/pre\>#s', create_function('$matches', 'return "<" . "pre>".htmlentities($matches[1])."</pre" .">";'), $content);
	$filter_code_content = preg_replace_callback('#\<code>(.+?)\<\/code\>#s', create_function('$matches', 'return "<" . "code>".htmlentities($matches[1])."</code" .">";'), $filter_pre_content);
	return $filter_code_content;
}
add_filter ('bbp_get_topic_content', 'bbp_replace_brackets', 9);
add_filter ('bbp_get_reply_content', 'bbp_replace_brackets', 9);

To implement this technique, add the code to your theme’s functions.php and done. Once in place, this function will ensure that pre and code tags are not removed/encoded/escaped by bbPress.

If you are not concerned about code tags, here is a simpler function that will ensure that pre tags are allowed in forum posts:

function bbp_allow_pre_tags(){
	global $allowedtags;
	$allowedtags['pre'] = array();
}
add_action('init', 'bbp_allow_pre_tags');

No editing is required for either of the functions presented in this tutorial to work. Just plug ’n play, and be sure to test the results.

Related

Learn more

Digging Into WordPressWordPress Themes In DepthWizard’s SQL Recipes for WordPress