esc_html_x( string $text, string $context, string $domain = ‘default’ ): string

Translates string with gettext context, 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.

Parameters

$textstringrequired
Text to translate.
$contextstringrequired
Context information for the translators.
$domainstringoptional
Text domain. Unique identifier for retrieving translated strings.
Default 'default'.

Default:'default'

Return

string Translated text.

Source

function esc_html_x( $text, $context, $domain = 'default' ) {
	return esc_html( translate_with_gettext_context( $text, $context, $domain ) );
}

Changelog

VersionDescription
2.9.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    The _x functions (like esc_html_x) are essentially the same as their _e counterparts, with an added “context” argument to explain the context a word or phrase is used in. Useful for words with multiple meanings:

    <!-- Here, we're asking the user to comment (verb): -->
    <a href="#comment">
      <?php 
        echo esc_html_x( 
          'Comment', 
          'Verb: To leave a comment', // Here's the contextual help
          'wpdocs_my_theme' 
        ); 
      ?>
    </a>
    <!-- Here, we're simply adding a heading with the text "Comment" (noun) -->
    <h3>
      <?php 
        echo esc_html_x( 
          'Comment', 
          'Noun: An individual comment', // Here's the contextual help
          'wpdocs_my_theme' 
        ); 
      ?>
    </h3>

You must log in before being able to contribute a note or feedback.