wppaste
WordPress

esc_html__( string $text, string $domain = 'default' ): string

Since
2.8.0
Source
wp-includes/l10n.php:338

Runs $text through translate() for the given $domain, then passes the result through esc_html() before returning it. Use it whenever a translated string is being placed directly into HTML markup, such as a heading or table cell, rather than into an attribute. If the text domain named by $domain isn't loaded, the original $text is escaped and returned unchanged rather than throwing an error. Reach for esc_html_e() instead if you want the string echoed immediately rather than returned.

Retrieves the translation of $text and escapes it for safe use in HTML output.

Description

If there is no translation, or the text domain isn't loaded, the original text is escaped and returned.

Compatibility

WordPress
since 2.8.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$textstring
Text to translate.
$domainstringoptional
Text domain. Unique identifier for retrieving translated strings.
Default 'default'.Default: 'default'

Return value

string
Translated text.

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Print a translation-ready heading in a plugin admin page

A plugin settings page needs a heading that is both translatable and safe to place directly into HTML.

$heading = esc_html__( 'Manage Featured Posts', 'my-plugin' );

echo '<h2>' . $heading . '</h2>';

The text domain 'my-plugin' isn't loaded in the sandbox, so the original English string is escaped and returned unchanged, which is exactly the fallback behavior the function documents.

Escape angle brackets in a translated string built from post meta

Post 2 in the baseline has a price meta value, and this builds a label that includes literal angle brackets around it.

$price = get_post_meta( 2, 'price', true );

$label = esc_html__( 'Current price: <' . $price . '>', 'my-plugin' );

echo $label;

esc_html() turns the '<' and '>' characters into HTML entities so they render as visible text instead of being interpreted as tags.

Common problems and fixes · 4

Why do the HTML tags inside my esc_html__ string show up as text instead of rendering?

esc_html__() calls esc_html() on the result of translate(), and esc_html() converts every angle bracket to an entity. Any markup embedded in $text is stripped of meaning, not just dangerous input.

Should I use esc_html__ or esc_html_e for output?

esc_html__() only returns the escaped, translated string, per the function's return type of string. It does not print anything by itself, so calling it alone and expecting text on the page will do nothing.

Why doesn't my string show up in the .pot file when I run it through esc_html__?

translate(), which esc_html__() calls internally, expects $text as a literal string because translation tools parse source code statically rather than executing it.

Why did my string come back untranslated even though I set a text domain?

The description notes that if the text domain named by $domain isn't loaded, the original $text is escaped and returned as is. A typo in the domain name or a missing load_plugin_textdomain() call produces the same silent fallback.

Alternatives and related functions

esc_html_e
When you want the escaped, translated string echoed immediately instead of returned for further use.
esc_html
When the text is already translated (or never needs translation) and only escaping for HTML output is required.
esc_attr__
When the translated string is going into an HTML attribute value rather than into element content.
translate
When you need the raw translated string without any HTML escaping applied.

Performance profile

How much work a call to esc_html__() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
10

Executed per call on PHP 8.5. The body compiles to 10.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
33

33 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()one call below esc_html__()

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost esc_html__() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always10translate(), esc_html()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 10 instructions, 10 executed per call, 0 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 2

Used by · 33

Show all 33

Source code

function esc_html__( $text, $domain = 'default' ) {	return esc_html( translate( $text, $domain ) );}

Changelog

Introduced in 2.8.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/l10n.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.