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.
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?
- Why does nothing get returned when I call esc_attr_e()?
- Should I use esc_attr_e() for dynamic values like post meta or form input?
Why isn't my string being translated even though I passed a text domain?
Why does nothing get returned when I call esc_attr_e()?
Should I use esc_attr_e() for dynamic values like post meta or form input?
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
- Scaling
- Constant
- Instructions
- 11
- Plugin surface
- None
- Called by
- 45
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 11.
Nothing here hands control to plugin code.
45 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 11 | translate(), 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
- esc_attr()Escaping for HTML attributes.
- translate()Retrieves the translation of $text.
Used by · 45
- Custom_Background::admin_page()Displays the custom background page.
- Custom_Image_Header::step_1()Displays first step of custom header image page.
- Twenty_Twenty_One_Dark_Mode::the_html()Prints the dark-mode switch HTML.
- WP_Admin_Bar::_render()
- WP_Customize_Header_Image_Control::render_content()Renders the control's content.
- WP_Customize_Manager::render_control_templates()Renders JS templates for all registered control types.
- WP_Customize_Nav_Menu_Control::content_template()JS/Underscore template for the control UI.
- WP_Customize_Nav_Menu_Location_Control::render_content()Render content just like a normal select control.
- WP_Customize_Nav_Menus::print_custom_links_available_menu_item()Prints the markup for available menu item custom links.
- WP_Customize_Themes_Panel::render_template()An Underscore (JS) template for rendering this panel's container.
- WP_Customize_Themes_Section::render_template()Renders a themes section as a JS template.
- WP_Media_List_Table::views()Overrides parent views to use the filter bar display.
Show all 45
- WP_Screen::render_screen_meta()Renders the screen's help section.
- WP_Theme_Install_List_Table::theme_installer()Prints the wrapper for the theme installer.
- WP_Widget_Area_Customize_Control::render_content()Renders the control's content.
- _WP_Editors::wp_link_dialog()Dialog for internal linking.
- attachment_submit_meta_box()Displays attachment submit form fields.
- display_setup_form()Displays installer setup form.
- find_posts_div()Outputs the modal window used for attaching media to posts or pages in the media-listing screen.
- install_plugins_favorites_form()Shows a username form for the favorites page.
- link_categories_meta_box()Displays link categories form fields.
- link_submit_meta_box()Displays link create form fields.
- list_plugin_updates()Display the upgrade plugins form.
- list_theme_updates()Display the upgrade themes form.
- list_translation_updates()Display the update translations form.
- login_footer()Outputs the footer for the login page.
- media_upload_form()Outputs the legacy media upload form.
- media_upload_gallery_form()Adds gallery form to upload iframe.
- post_submit_meta_box()Displays post submit form fields.
- post_tags_meta_box()Displays post tags form fields.
- print_embed_sharing_button()Prints the necessary markup for the embed sharing button.
- print_embed_sharing_dialog()Prints the necessary markup for the embed sharing dialog.
- request_filesystem_credentials()Displays a form to the user to request for their FTP/SSH details in order to connect to the filesystem.
- signup_another_blog()Shows a form for returning users to sign up for another site.
- signup_blog()Shows a form for a user or visitor to sign up for a new site.
- signup_user()Shows a form for a visitor to sign up for a new user account.
- twentyeleven_settings_field_link_color()Renders the Link Color setting field.
- wp_dashboard_quick_press()Displays the Quick Draft widget.
- wp_image_editor()Loads the WP image-editing interface.
- wp_nav_menu_item_link_meta_box()Displays a meta box for the custom links menu item.
- wp_nav_menu_item_post_type_meta_box()Displays a meta box for a post type menu item.
- wp_nav_menu_item_taxonomy_meta_box()Displays a meta box for a taxonomy menu item.
- wp_print_community_events_markup()Prints the markup for the Community Events section of the Events and News Dashboard widget.
- wp_print_media_templates()Prints the templates used in the media manager.
- wp_print_revision_templates()Print JavaScript templates required for the revisions experience.
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.
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.