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.
Lessons
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.
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.
| Output context | Function | Example |
|---|---|---|
| HTML text | esc_html() | <h2><?php echo esc_html( $name ); ?></h2> |
| HTML attribute | esc_attr() | <input value="<?php echo esc_attr( $value ); ?>"> |
URL in href or src | esc_url() | <a href="<?php echo esc_url( $link ); ?>"> |
<textarea> content | esc_textarea() | <textarea><?php echo esc_textarea( $bio ); ?></textarea> |
| HTML that may contain safe tags | wp_kses_post() | <?php echo wp_kses_post( $intro ); ?> |
| Inline JavaScript data | wp_json_encode() | Inside wp_add_inline_script() |
<?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.
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 of | Use |
|---|---|
_e( 'Read more', 'gdc-starter' ) | esc_html_e( 'Read more', 'gdc-starter' ) |
echo __( 'Search', 'gdc-starter' ) in an attribute | esc_attr_e( 'Search', 'gdc-starter' ) |
__() inside printf() | esc_html__() |
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 );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.
defined( 'ABSPATH' ) || exit; so it cannot be run directly by URL.extract(), eval() or create_function().echo. Each one should be wrapped in an escaping function or print a value WordPress already escaped._e() and __() in templates with esc_html_e() / esc_html__() / esc_attr_e().<img src=x onerror=alert(1)> and confirm your theme shows it as text everywhere.In accordance with Section 6 & Section 7 of the DPDP Act 2023, third-party analytics and marketing trackers are strictly gated behind your affirmative consent. You may withdraw consent at any time under Section 6(4).
Essential for session authentication (ci_session), Cross-Site Request Forgery security (csrf_cookie_name), and Cloudflare DDoS/bot prevention. Cannot be disabled.
Gated under DPDP Section 7. Allows Google Analytics 4 (Google Consent Mode v2) to measure aggregate, anonymized tool usage.
Enables browser local storage to preserve your theme selection (dark/light mode) and code editor layout between visits.
Used to display developer sponsorships and native tool partner notices.