Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk2 min

Why wp_kses Removes Your HTML—and How to Allow the Right Tags

When wp_kses removes expected markup, inspect the rules passed to it: the tag, required attributes, values, and whether a plugin or theme changes the selected context.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

wp_kses() removes markup that is not permitted by the allow-list or context you pass to it. To find out why a tag disappeared, check the actual call, then check whether its lowercase tag name, required attributes, and any needed attribute values are allowed. The function returns filtered HTML; it does not explain which rule removed a particular part.

Start with the call that filters the HTML

Compare the exact string immediately before and after wp_kses(), and locate the call site that produced the change. Its second argument determines the rules: it may be an explicit array or a named context. The function expects unslashed input.

As an Amazon Associate I earn from qualifying purchases.

For example, wp_kses($html, 'post') uses the post context, while wp_kses($html, $allowed_html) applies the rules in the array. WordPress documents both forms in the wp_kses() reference.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check whether the element or an attribute was removed

KSES filters elements, attributes, attribute values, and entities. A tag can survive while an attribute is removed, or the whole element can disappear if it is not permitted. Allowing a tag does not automatically allow every attribute on it.

With an explicit allow-list, add the needed tag and only the attributes the output requires. Tag and attribute names in the rules must be lowercase; mixed- or uppercase entries are not recognized as permitted. The wp_kses_allowed_html() reference describes retrieving rules for a context, and the WordPress handbook shows a custom allow-list with selected tags and attributes in its Escaping Data – Common APIs Handbook.

Choose an explicit allow-list or a named context

Choice When it fits Where to inspect rules
Explicit allow-list When the output needs a specific subset of HTML that does not match a named context. Inspect the array passed as the second argument to wp_kses().
Named context When the rules for an existing context fit the content being filtered. Use wp_kses_allowed_html() to retrieve the context rules. The wp_kses_allowed_html filter can customize them.

For a named context, check the rules actually in effect rather than assuming the defaults: a plugin or theme may change them through the documented wp_kses_allowed_html filter. The context reference explains the available retrieval function.

Use the post wrapper only when post rules fit

wp_kses_post() applies the post context: WordPress documents it as calling wp_kses($data, 'post'). Use it when that context matches the HTML you intend to keep. If the needed markup is narrower or different, pass an appropriate custom allow-list or context to wp_kses() instead. See the wp_kses_post() reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Keep input slashing separate from output escaping

Direct calls to wp_kses() and wp_kses_post() expect unslashed data. Do not confuse them with wp_filter_post_kses(), whose reference specifies slashed input and describes stripping and restoring slashes around the call. That separate contract is not the contract for direct wp_kses() use; see the wp_filter_post_kses() reference.

Sanitizing permitted HTML does not remove the need to escape at output time. WordPress’s handbook says, “You always want to escape when you echo, not before.” When HTML should remain, use KSES with an appropriate context or selected tags; when the output is plain text, use escaping suited to that output context. The handbook discusses escaping data and common APIs.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.