Search code examples
phpwordpressfiltersanitizationshortcode

What characters are allowed as a shortcode tag and what sanitizing filter should I used for them?


I working on a plugin where the user can define shortcode tags. What would you suggest to allow, my thought is only allow ascii characters.

Also, how do you sanitize the input? I'm thinking maybe stip_tags and then regex to allow only a-z, 0-9 or is there a better solution? Maybe wordpress filter would work? Or could I maybe use the filter wordpress uses for slugs?


Solution

  • this-is-a-good-slug Meaning you must allow also an hyphen (-)

    after this you need to call the following:

    $string = preg_replace("/[^a-z0-9_\s-]/","",$string);    // strip away everything except a-z,0-9 and hyphen
    $string = preg_replace("/[\s-]+/"," ",$string);     // clean multiple dashes or whitespaces
    $string = preg_replace("/[\s_]/","-",$string);           // convert whitespaces and underscore to dash
    

    be careful; you need ALSO to change UTF8 character into ascii/latin, meaning you NEED to transliterate in other languages containing such characters.