WordPress wp_kses() Deep Dive: Custom Allowed HTML Maps, Protocols, and Secure Output

wp_kses(): allow exactly the HTML you need

strip_tags() removes all HTML, including the bold text and links your users wanted to keep. wp_kses() works the other way round: it keeps only the tags and attributes on your list and removes everything else, including event attributes like onclick and dangerous URL protocols like javascript:. Core uses it for comments and for post content of users without the unfiltered_html capability.

Problem: Users can enter a short note with some formatting and links. wp_kses_post() allows far more than needed (headings, tables, images, inline styles), and strip_tags() removes the formatting completely.

Solution: Pass your own list of allowed tags and attributes to wp_kses(), and if needed a list of allowed protocols as the third argument. Use the same list when saving and when printing.

The allowed list

The keys are tag names, the values are arrays of allowed attributes (true = any value):

<?php
function ha_note_allowed_html() {
    return array(
        'strong'     => array(),
        'em'         => array(),
        'code'       => array(),
        'br'         => array(),
        'p'          => array(),
        'ul'         => array(),
        'ol'         => array(),
        'li'         => array(),
        'blockquote' => array( 'cite' => true ),
        'a'          => array(
            'href'  => true, // checked against the allowed protocols
            'title' => true,
        ),
    );
}

Everything not in the list is removed, the text inside removed tags is kept: <h1>Title</h1> becomes “Title”.

Saving and printing

<?php
// On save
$note = wp_kses( wp_unslash( $_POST['ha_note'] ?? '' ), ha_note_allowed_html() );
update_post_meta( $post_id, '_ha_note', $note );

// On output: the same filter again
echo wp_kses( get_post_meta( $post_id, '_ha_note', true ), ha_note_allowed_html() );

Filtering again on output is cheap and protects against values that reached the database another way: an import, an older version of your code, a direct SQL update.

Protocols

By default links may use the protocols from wp_allowed_protocols(): http, https, mailto, tel, ftp and several more. For one call you can restrict them with the third argument:

<?php
$clean = wp_kses( $html, ha_note_allowed_html(), array( 'http', 'https' ) );

Do not change the global list with the kses_allowed_protocols filter for this: it affects every use of kses on the site, including post content and comments.

The ready-made lists

<?php
// Everything that is allowed in post content for authors
$safe = wp_kses_post( $html );

// The short list allowed in comments: a, abbr, b, blockquote, cite, code, em, i, q, strike, strong...
$safe = wp_kses_data( $html );

// The list itself, to extend it
$allowed          = wp_kses_allowed_html( 'post' );
$allowed['iframe'] = array( 'src' => true, 'width' => true, 'height' => true, 'allowfullscreen' => true );

Neither list contains script, iframe, form or style tags. Adding iframe as above allows embedding any site, so do it only for trusted users. A typical problem with HTML that was not filtered is shown in a layout broken by HTML in ACF fields.

NOTE: wp_kses() is for HTML that is meant to be HTML. For plain text, use sanitize_text_field() on save and esc_html() on output; it is simpler and faster. And kses is slow on large documents, so do not run it on every request over big chunks of content that you could filter once when saving.