Back to Snippets

WordPress Output Escaping Cheat Sheet

PHP Security April 6, 2026

Complete WordPress output escaping reference — esc_html, esc_attr, esc_url, esc_js, wp_kses_post. Use the right function for every context.

Snippet Stats

Lines 25
Characters 690
Read 1 min
php • 25 lines
// HTML context — use for any text displayed in HTML.
echo esc_html( $user_input );

// Attribute context — use inside HTML attributes.
echo '';

// URL context — use for href, src, action attributes.
echo '';

// JavaScript context — use inside inline JS.
echo '';

// Rich HTML — allows safe tags (p, br, strong, em, a, etc.).
echo wp_kses_post( $editor_content );

// Custom allowed HTML tags.
echo wp_kses( $content, array(
    'a'      => array( 'href' => true, 'title' => true, 'rel' => true ),
    'strong' => array(),
    'em'     => array(),
) );

// Translation + escape in one call.
esc_html_e( 'Settings saved.', 'theme-domain' );
echo esc_html__( 'Hello, %s', 'theme-domain' );

Found an issue with this snippet? Help us improve by reporting it. Report it →

Related Snippets

View all
JavaScript
This React higher order component handles authentication and authorization by checking user credentials before rendering the wrapped component. It uti...

React Higher Order Component For Authentication And Authorization In Large Scale Applications

JavaScript
This React higher-order component handles authentication and authorization by wrapping the provided component with authentication checks, ensuring onl...

React Higher Order Component For Role Based Authentication And Authorization

PHP
This snippet provides a custom WordPress plugin that utilizes hooks to modify the user registration process, allowing for additional validation and cu...

WordPress Custom Plugin Hooks For User Registration