Results 1 to 5 of 5

Thread: Validating HTML in WordPress forms

  1. #1
    Ryan's Avatar
    Ryan is offline WordPress Legend
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    2,797

    Default Validating HTML in WordPress forms

    I just tried to use wp_filter_kses() to strip HTML out of some data in a plugins' form, but it seems to be stripping out most of the HTML I need :(

    I need it to preferably allow the insertion of <div>, <li>, <a> and <span> tags, but only the <a> tags seem to be allowed via wp_filter_kses().

    This is only for site admins to be using, so there's no issue with people borking the layout or anything like that. I just need to make sure there's no security issues to contend with so I'm guessing I just need to strip out the <script> tags. I can find plenty of information about best practices for this in PHP in general, but nothing for WordPress specifically. Any ideas on the best approach to filtering this in WordPress in a secure way?

    Thanks :)

  2. #2
    Ryan's Avatar
    Ryan is offline WordPress Legend
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    2,797

    Default

    FYI, I'm currently using strip_tags( $input, '<a><div><li><span>' ); which seems to do what I'm looking for. I just want to check there's not a built in WP way of doing the same thing that I should be using instead.

  3. #3
    andreasnrb's Avatar
    andreasnrb is offline Kegger
    Join Date
    Jun 2009
    Posts
    594

    Default

    http://codex.wordpress.org/Function_Reference/wp_kses
    PHP Code:
    $allowed_html=array('a' => array('href' => array(),'title' => array()),'br' => array(),'em' => array(),'strong' => array());

    wp_kses($string$allowed_html$allowed_protocols); 

  4. #4
    Ryan's Avatar
    Ryan is offline WordPress Legend
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    2,797

    Default

    Awesome, thanks :) I figured there was probably some way to do it that I'd glanced over and it seems I was correct.

    That SEO security topic got me thinking I should tighten a few of these things up a little :) I've been making too many assumptions about how data will be handled in the past.

  5. #5
    Ryan's Avatar
    Ryan is offline WordPress Legend
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    2,797

    Default

    I was about to ask what advantage there was in using wp_kses() over strip_tags(), but I found the answer already ... http://phpxref.com/xref/wordpress/wp...p.html#wp_kses
    This function makes sure that only the allowed HTML element names, attribute
    names
    and attribute values plus only sane HTML entities will occur in
    $string. You have to remove any slashes from PHP's magic quotes before you
    call this function.

    The default allowed protocols are 'http', 'https', 'ftp', 'mailto', 'news',
    'irc', 'gopher', 'nntp', 'feed', and finally 'telnet. This covers all common
    link protocols, except for 'javascript' which should not be allowed for
    untrusted users.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •