Filters text content and strips out disallowed HTML.
Description
This function makes sure that only the allowed HTML element names, attribute names, attribute values, and HTML entities will occur in the given text string.
This function expects unslashed data.
See also
- wp_kses_post(): for specifically filtering post content and fields.
- wp_allowed_protocols(): for the default allowed protocols in link URLs.
Parameters
$content
stringrequired- Text content to filter.
$allowed_html
array[]|stringrequired- An array of allowed HTML elements and attributes, or a context name such as
'post'
. See wp_kses_allowed_html() for the list of accepted context names. $allowed_protocols
string[]optional- Array of allowed URL protocols.
Defaults to the result of wp_allowed_protocols() .Default:
array()
Source
function wp_kses( $content, $allowed_html, $allowed_protocols = array() ) {
if ( empty( $allowed_protocols ) ) {
$allowed_protocols = wp_allowed_protocols();
}
$content = wp_kses_no_null( $content, array( 'slash_zero' => 'keep' ) );
$content = wp_kses_normalize_entities( $content );
$content = wp_kses_hook( $content, $allowed_html, $allowed_protocols );
return wp_kses_split( $content, $allowed_html, $allowed_protocols );
}
Changelog
Version | Description |
---|---|
1.0.0 | Introduced. |
Many function names in WordPress are self-explanatory and if they aren’t, their documentation usually sheds some light on how they got their name. I find this makes it easier to later recall their names and uses. However,
wp_kses
is an exception. So for anyone else wondering:kses
comes from the terms XSS (cross-site scripting) and access. It’s also a recursive acronym (every open-source project should have one!) for “kses strips evil scripts”.Allowed HTML tags array
This is an example of how to format an array of allowed HTML tags and attributes.
array( 'a' => array( 'href' => true, 'title' => true, ), 'br' => array(), 'em' => array(), 'strong' => array(), );
WordPress wp_kses is an HTML filtering mechanism. If you need to escape your output in a specific (custom) way, wp_kses function in WordPress will come handy.
Output:
Before
wp_kses
: Check Kses function I am stronger and cooler every single day Click HereAfter
wp_kses
: String using wp_kses function…. Check Kses function I am stronger and cooler every single day Click HereIt will display a resultant string as shown in the output screen. It only reflects the allowed tags
strong
,br
,p
as defined inwp_kses
function and anchor tag is removed. So, no link for click Here text is formed.See
wp_kses_allowed_html()
and /wp-includes/kses.php to get a list of the possible default values of the allowed HTML tags.If you want to keep certain style properties you have to use another filter.
Unortunately wp_kses will check the style properties against a list of allowed properties and it will still strip the style attribute if none of the styles are safe.
E.g. Use this filter if you want to keep the `display` property within a `style`:
a
Check kses.php for default:
https://core.trac.wordpress.org/browser/trunk/src/wp-includes/kses.php
Sanitize SVG markup for front-end display using
wp_kses
, and a list of allowed HTML elements and attributes specific to a SVG tag.If you are using wp_kses to escape SVG, be warned `wp_kses() ` will strip camelcased attributes in your args. Make sure your args are converted to lowercase for their uppercase equivalents. For example:
Allowed HTML elements attributes don’t need to be empty arrays, but simply a boolean,