函数文档

_e()

💡 云策文档标注

概述

_e() 是 WordPress 中用于输出翻译文本的函数,它直接回显翻译后的字符串,常用于前端显示。该函数基于 translate() 实现,支持指定文本域以管理多语言文件。

关键要点

  • _e() 函数用于输出翻译文本,相当于 echo translate(),适用于需要直接显示的场景。
  • 参数包括必需的 $text(要翻译的文本)和可选的 $domain(文本域,默认为 'default'),用于标识翻译文件。
  • 函数内部调用 translate( $text, $domain ) 获取翻译,然后通过 echo 输出,简化了翻译和输出的流程。
  • 在安全方面,_e() 不进行转义,建议使用 esc_html_e() 或结合 __() 与转义函数(如 esc_html())来防止 XSS 攻击。
  • 文本域应与主题或插件的文件夹名一致,以便正确加载对应的 .mo 翻译文件。

代码示例

_e( 'Hello World', 'my-text-domain' );

注意事项

  • 避免直接使用 _e() 输出未转义的动态内容,优先使用 esc_html_e() 或类似转义函数以确保安全性。
  • 确保 $domain 参数与翻译文件匹配,否则可能无法正确加载翻译。

📄 原文内容

Displays translated text.

Parameters

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

Source

function _e( $text, $domain = 'default' ) {
	echo translate( $text, $domain );
}

Changelog

Version Description
1.2.0 Introduced.

User Contributed Notes

  1. Skip to note 9 content

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

    In this function,the “$domain” is used to locate the multilingual file.(The file which is the translation of your theme.)
    In wp-include there is a function related to this var

    function load_theme_textdomain( $domain, $path = false ) {
    $locale = apply_filters( 'theme_locale', get_locale(), $domain );
     
    if ( ! $path )
    $path = get_template_directory();
     
    // Load the textdomain from the Theme provided location, or theme directory first
    $mofile = "{$path}/{$locale}.mo";
    if ( $loaded = load_textdomain($domain, $mofile) )
    return $loaded;
     
    // Else, load textdomain from the Language directory
    $mofile = WP_LANG_DIR . "/themes/{$domain}-{$locale}.mo";
    return load_textdomain($domain, $mofile);
    }

    You can see $domain is used in this file.Actually it’s used to locate the .mo file.So you must may sure it share the same name with your theme folder name

  2. Skip to note 11 content

    Important security note: _e() is not an escaping function. Since all output really needs to be escaped, consider using __() instead.

    For example, this is safe:
    echo esc_html( __( 'This is my translatable text' ) )

    But this might not be, because the output can get filtered and is not escaped close to where it is output:
    _e( 'This is my translatable text' )