SponsoredHosinger Special Offer

WP WordPress Theme Development

Build classic and block WordPress themes from scratch: the template hierarchy, The Loop, the_title() and the_content(), template parts, menus, the Customizer, child themes and theme.json.

Escaping and Security in Themes

WordPress Theme Development Lesson 25 of 30 ~3 min read

Overview

Escaping means converting data into a safe form for the exact place it is printed — HTML text, an attribute, a URL — and in a WordPress theme every value that came from the database or a visitor must be escaped at the moment it is output.

Why themes get this wrong

It is tempting to think "only admins write post titles, so titles are safe". But a title can be set by an Author account, imported from a feed, or written by a plugin. Cross-site scripting (XSS) happens when such a value contains <script> and your theme prints it raw. The fix is a habit, not a judgement call: escape late, escape every time.

The escaping functions

Output contextFunctionExample
HTML textesc_html()<h2><?php echo esc_html( $name ); ?></h2>
HTML attributeesc_attr()<input value="<?php echo esc_attr( $value ); ?>">
URL in href or srcesc_url()<a href="<?php echo esc_url( $link ); ?>">
<textarea> contentesc_textarea()<textarea><?php echo esc_textarea( $bio ); ?></textarea>
HTML that may contain safe tagswp_kses_post()<?php echo wp_kses_post( $intro ); ?>
Inline JavaScript datawp_json_encode()Inside wp_add_inline_script()

Escape late — at the echo

<?php
// Wrong: escaped early, then used in a different context.
$title = esc_html( get_the_title() );
echo '<a title="' . $title . '">';           // esc_html is the wrong escaper for an attribute.

// Right: keep raw data in variables, escape exactly where it is printed.
$title = get_the_title();
echo '<a title="' . esc_attr( $title ) . '">';
echo '<h2>' . esc_html( $title ) . '</h2>';

Escaping at the echo lets a reviewer verify safety by looking at one line, without tracing where a variable came from.

Translated strings

Translations come from files a third party wrote. WordPress gives you combined translate-and-escape functions, and you should use them for every string in a template:

Instead ofUse
_e( 'Read more', 'gdc-starter' )esc_html_e( 'Read more', 'gdc-starter' )
echo __( 'Search', 'gdc-starter' ) in an attributeesc_attr_e( 'Search', 'gdc-starter' )
__() inside printf()esc_html__()

Allowing some HTML with wp_kses

Sometimes a value legitimately contains formatting — a customizer field where the owner may add a link. wp_kses() keeps only the tags and attributes you list:

<?php
$allowed = array(
    'a'      => array(
        'href'   => true,
        'target' => true,
        'rel'    => true,
    ),
    'strong' => array(),
    'em'     => array(),
);

echo wp_kses( get_theme_mod( 'gdc_starter_footer_html', '' ), $allowed );

What does not need escaping

Template tags that print their own complete, escaped output — the_title(), the_content(), the_permalink(), body_class(), wp_nav_menu(), the_post_thumbnail() — are already safe for their usual context. The rule is about your echo statements.

Other theme security rules

  • Start every PHP file that is not a template with defined( 'ABSPATH' ) || exit; so it cannot be run directly by URL.
  • Never use extract(), eval() or create_function().
  • A theme should not process forms or write to the database. That is plugin territory, where nonces and capability checks belong.
  • Load external resources over HTTPS, and only from sources you trust.

Practice

  • Search your theme for every echo. Each one should be wrapped in an escaping function or print a value WordPress already escaped.
  • Replace every _e() and __() in templates with esc_html_e() / esc_html__() / esc_attr_e().
  • Create a post titled <img src=x onerror=alert(1)> and confirm your theme shows it as text everywhere.
Create a free account to save which lessons you've finished. Save my progress