--- name: output-escaping description: > Use when echoing or printing any dynamic value in WordPress PHP or templates — into HTML, attributes, URLs, inline JavaScript, or textareas. Escapes at the point of output with esc_html, esc_attr, esc_url, esc_js, esc_textarea, or wp_kses_post, including the i18n variants (esc_html__, esc_attr_e). Prevents stored and reflected XSS. Apply proactively to every echoed variable, even data from the database. compatibility: "Examples generally use PHP 7.4 syntax; check each API against target WordPress/PHP versions. Use maintained WordPress and supported PHP in production. Shell examples require their named tools." license: MIT metadata: tags: "wordpress, security, php, escaping, xss, output" --- # Output escaping (XSS prevention) ## When to use this skill Use this skill whenever a **dynamic value is sent to the browser**: - `echo` / `print` of any variable into HTML. - Values placed into HTML attributes (`value`, `href`, `src`, `class`, `data-*`). - URLs in `href` / `src` / redirects. - Data injected into inline `'; ``` ```php // ✅ Secure: esc_js for a single quoted string... echo ''; // ...or, better, pass structured data as JSON: echo ''; ``` ### Mistake 5 — Using `wp_kses_post()` where escaping is required (and vice versa) ```php // ❌ Wrong tool: kses lets markup through where you wanted plain text. echo '' . wp_kses_post( $plain_title ) . ''; ``` ```php // ✅ Plain text → esc_html; intentional rich HTML → wp_kses_post. echo '' . esc_html( $plain_title ) . ''; echo '
' . wp_kses_post( $rich_bio ) . '
'; ``` ### Mistake 6 — Translated strings echoed unescaped ```php // ❌ Insecure: translation files can contain markup; raw echo into a tag. echo '

' . __( 'Welcome, %s', 'my-plugin' ) . '

'; ``` ```php // ✅ Secure: escape the translated output. printf( '

%s

', esc_html__( 'Welcome back', 'my-plugin' ) ); ``` ### Mistake 7 — Redirect target not validated ```php // ❌ Insecure: open redirect to an attacker-controlled site. $redirect = $_GET['redirect_to']; wp_redirect( $redirect ); exit; ``` ```php // ✅ Secure: the "escape" for a Location target is validation + wp_safe_redirect. $redirect = isset( $_GET['redirect_to'] ) ? esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ) : ''; $redirect = wp_validate_redirect( $redirect, admin_url() ); wp_safe_redirect( $redirect ); exit; ``` ### Mistake 8 — Using wp_kses_post where a tighter allowlist is needed ```php // ❌ Risky: wp_kses_post allows many tags you may not want in a caption. echo wp_kses_post( $caption ); ``` ```php // ✅ Secure: define a custom allowlist when the context is narrower. $allowed = array( 'a' => array( 'href' => array() ), 'em' => array(), 'strong' => array(), ); echo wp_kses( $caption, $allowed ); ``` Related: see the `input-sanitization-validation` skill for validating redirect URLs on input and the `filesystem-security` skill for path escaping. ### Mistake 9 — Escaping in PHP, then re-injecting in JavaScript ```js // ❌ Insecure: PHP passed the value safely through wp_localize_script or // wp_json_encode, but the script decodes it into markup. const name = myPluginData.displayName; document.querySelector( '#greeting' ).innerHTML = 'Hello ' + name; jQuery( '#title' ).html( myPluginData.title ); ``` ```js // ✅ Secure: insert untrusted values as text in the DOM. document.querySelector( '#greeting' ).textContent = 'Hello ' + myPluginData.displayName; jQuery( '#title' ).text( myPluginData.title ); ``` Server-side escaping protects the transfer into the page, not what client code does next. Review DOM sinks (`innerHTML`, `outerHTML`, `insertAdjacentHTML`, `document.write`, jQuery `.html()`/`.append()` with strings, `location` assignments) in plugin scripts. ## Correct code examples A complete context → function reference (with `esc_url` vs `esc_url_raw`, the i18n variants, and `wp_kses` allowlist usage) is in [`references/escaping-cheatsheet.md`](references/escaping-cheatsheet.md). ```php // A small template snippet that escapes every dynamic value in context. ?>