wppaste
WordPress

esc_attr_e( string $text, string $domain = 'default' )

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

Echoes a translated string after running it through esc_attr() so it is safe to print inside an HTML attribute value. Use it for static, translatable label or title text, not for dynamic data like post meta or user input, which should go through esc_attr() directly. Pair it with esc_attr__() when you need the escaped string in PHP instead of printing it immediately.

Displays translated text that has been escaped for safe use in an attribute.

Description

Encodes < > & " ' (less than, greater than, ampersand, double quote, single quote).
Will never double encode entities.

If you need the value for use in PHP, use esc_attr__().

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'

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 translatable title attribute on a post link

Build an anchor tag around post 1 and put a translatable string in its title attribute.

$post = get_post( 1 );

echo '<a href="' . esc_url( get_permalink( $post ) ) . '" title="';
esc_attr_e( 'Read the full post', 'my-plugin' );
echo '">' . esc_html( $post->post_title ) . '</a>';

Print a translatable placeholder for a post meta input field

Render a simple text input for the price meta on post 2, using esc_attr_e for the label and placeholder text.

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

echo '<label for="price">';
esc_attr_e( 'Price', 'my-plugin' );
echo '</label> ';

echo '<input type="text" id="price" name="price" placeholder="';
esc_attr_e( 'Enter price in USD', 'my-plugin' );
echo '" value="' . esc_attr( $price ) . '">';

The stored price value goes through esc_attr(), not esc_attr_e(), because it is data, not a translatable string.

Common problems and fixes · 3

Why isn't my string being translated even though I passed a text domain?

esc_attr_e() calls translate( $text, $domain ) internally, so if $domain doesn't match the text domain your plugin or theme actually loaded, translate() falls back to the original English string with no error.

Why does nothing get returned when I call esc_attr_e()?

esc_attr_e() only echoes the escaped string; it has no return statement, so assigning $value = esc_attr_e( $text ) always gives you an empty or unset variable.

Should I use esc_attr_e() for dynamic values like post meta or form input?

esc_attr_e() runs its argument through translate() before escaping, which is meant for static translatable labels, not for user-entered or database values that shouldn't be run through translation lookup.

Alternatives and related functions

esc_attr
When you need to escape a dynamic value, such as post meta or user input, rather than a translatable string.
esc_attr__
When you need the escaped, translated string returned so you can store or reuse it in PHP instead of echoing it immediately.
esc_html_e
When the translated text is going into HTML element content rather than an attribute value.
_e
When the text is being echoed into a context that doesn't need attribute-specific escaping, such as plain page content.

Performance profile

How much work a call to esc_attr_e() 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
11

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
45

45 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_attr_e()

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_attr_e() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always11translate(), esc_attr()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 11 instructions, 11 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 · 45

Show all 45

Source code

function esc_attr_e( $text, $domain = 'default' ) {	echo esc_attr( 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 7.0.4 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.