Adding allowed HTML tags for WP comments

I couldn’t find any easy answer to this ques­tion myself when I setup cus­tom tags for com­ment­ing on this site a while back, and Indranil is try­ing to fig­ure it out as well, so I fig­ured I’d share a brief “how to” on the subject.

The default Word­Press com­ment­ing tags allowed are a, abbr, acronym, b, blockquote code, em, i, strike and strong. For myself, and Indranil (and prob­a­bly many other Word­Press users), there is a desire to allow users more free­dom in their com­ment­ing for­mat­ting: specif­i­cally, to allow the use of lists and list items.

Whilst this used to be pos­si­ble with the (dep­re­cated) my-hacks.php file, this is advised against as the “hacks” sys­tem has changed, and chances are that file will be gone in a few releases time. The eas­i­est way to add per­mit­ted tags is to edit the file which does the pro­cess­ing, kses.php. Kses is described in the file header as being a “HTML/XHTML fil­ter that only allows some ele­ments and attributes” — and that’s what it does.

If you open this file, located in the wp-includes direc­tory within your Word­Press instal­la­tion (full path to file should be /wp-includes/kses.php), and scroll down a lit­tle way, you should see a vari­able $allowedtags, with an array. (For me, it begins on line 17 of the file: yours may vary.)

By fol­low­ing the for­mat tags are already listed there in, you can prob­a­bly fig­ure out how it works: for many, it’s just a mat­ter of uncom­ment­ing the appro­pri­ate tags. I can’t recall if there are options for list tags in there already, I’m sorry: if you see lines which begin with

//'li' => array(), or
//'ol' => array(), or
//'ul' => array(),

then sim­ply delete the pre­ced­ing slashes (//). Save the file, and com­ment­ing with these tags is now permitted.

If the file didn’t include those com­mented out lines, then sim­ply add

'li' => array(),
'ol' => array(),
'ul' => array(),

to the array, and save.

There you have it! Of course, it’s pos­si­ble to per­mit attrib­utes and the like — take a look at the anchor (a) part of the array for an exam­ple of that — but for lists, that’s mostly unec­ces­sary (so far as I’ve seen, anyway).

Tags: , ,

posted on Saturday, October 30th, 2004 at 10:28 am by Josh, filed under Geek.

9 Responses to “Adding allowed HTML tags for WP comments”

  1. dale says:

    Heh. You should have asked me. I’ve been using kses behind the scenes for a while now. Great lit­tle script. The SF page can be found here. Although I am using the class based ver­sion instead of the func­tion based one in word­press. It is very nice to work with.

    The fol­low­ing is from the class documentation

    Parse($string = “”)
    Per­forms the actual pars­ing of the HTML/HTML, remov­ing
    tags and attrib­utes that aren’t allowed.

    Pro­to­cols()
    Accepts either a string or an array of pro­to­cols, and
    calls $this->AddProtocol appro­pri­ately. Pro­to­cols
    can option­ally end in “:”.

    AddProtocol($protocol = “”)
    Accepts a sin­gle string as a pro­to­col and attempts to
    place it in the list of allowed pro­to­cols. The pro­to­col
    can option­ally end in “:”.

    AddHTML($tag = “”, $attribs = array())
    Accepts a string indi­cat­ing an allowed tag, and a list
    of accept­able attrib­utes for the spe­cific tag, along
    with mod­i­fiers for that attribute. See the kses
    doc­u­men­ta­tion for details.

    Addi­tional calls to this method using the same tag will
    over­write the exist­ing tag definition.

  2. Indranil says:

    Thanks for the write-up man. Much appreciated.

    • Thank
    • you
  3. Joshua says:

    Dale: Yeah, I’ve been using it ever since this Word­Press site went pub­lic, so it’s been a while now, but infor­ma­tion was still a lit­tle thin on the ground! It’s pretty snazzy.

    Indranil: You’re wel­come! Inter­est­ing list, there ;) Hehehe.

  4. Ulf says:

    Nice write-up!

    // Ulf Harn­ham­mar (kses guy)

  5. The Dog says:

    Josh
    Great help! Thank you! I used your advice to fig­ure out how and where to add in an img tag for com­ments. It works great.

    ‘img’ => array(‘src’ => array()),

  6. oso says:

    Just wanted to say thanks for this write up. Really helped me out.

  7. oso says:

    Oops — I also wanted to add for future read­ers — don’t copy and paste the code you see above because the quote marks got con­verted into curly quotes which will give you PHP errors. Bet­ter to hand type it in.

  8. Josh says:

    @oso, thanks for the tip. I’ve edited the post so that the quote marks are defined with entity codes ('), which will hope­fully make it safe to copy and paste.

  9. Anonymous says:

    I send you and your vis­i­tors my best greetings.

Leave a Reply