Skip to:

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

  • _ck_


    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)

    Plugin Name: Strip Bad Markup
    Plugin URI:
    Description: removes tags that are not allowed in posts instead of encoding them
    Author: _ck_
    Author URI:
    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("/</?(?!($tags)).*?>/sim","", $text);
    return $text;


Viewing 2 replies - 1 through 2 (of 2 total)

  • johnhiler


    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.



    Ah I didn’t think of handling &nbsp;

    But I thought bbpress allowed htmlentities. Maybe only some of them.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Skip to toolbar