Forums

Join
bbPress Support ForumsPluginsHere's how to strip bad markup instead of encoding it

Info

Here's how to strip bad markup instead of encoding it

  1. On some forums you might want to remove bad html markup that's not allowed instead of encoding it into text. Here's a mini-plugin to do just that. It also converts <b> into <strong> and <i> into <em>

    It only runs when posts are saved instead of each time they are displayed to keep things at full speed. Posts by Moderators and Administrators are not stripped.

    (note: for now it doesn't obey backticks and will strip inside them)

    <?php
    /*
    Plugin Name: Strip Bad Markup
    Plugin URI: http://bbpress.org/plugins/topic/
    Description: removes tags that are not allowed in posts instead of encoding them
    Author: _ck_
    Author URI: http://bbShowcase.org
    Version: 0.0.1
    */
    
    add_filter('pre_post', 'strip_bad_markup',9);
    
    function strip_bad_markup($text) {
    if (!bb_current_user_can('moderate')) {
    	$tags=bb_allowed_tags(); $tags['br']=true; $tags['p']=true; $tags = implode('|',array_keys($tags));
    	$text=preg_replace(array("/<(\/)?b>/si","/<(\/)?i>/si"),array("<strong>","<em>"),$text);
    	$text=preg_replace("/<\/?(?!($tags)).*?>/sim","", $text);
    }
    return $text;
    }
    
    ?>
  2. This looks awesome! I am going to try it out tomorrow... I noticed that some nbsp's were being "encoded" in my bbPress installs tonight, and was wondering how I was gonna strip them out. Looking forward to trying this out :-).

    Will report back with results.

  3. Ah I didn't think of handling &nbsp;
    But I thought bbpress allowed htmlentities. Maybe only some of them.

  4. You must log in to post.