函数文档

esc_attr_e()

💡 云策文档标注

概述

esc_attr_e() 是一个 WordPress 函数,用于输出经过转义和翻译的文本,适用于 HTML 属性中的安全使用。它结合了翻译和转义功能,避免 XSS 攻击。

关键要点

  • 功能:输出转义后的翻译文本,适用于 HTML 属性,如 value、title 等。
  • 转义内容:编码特殊字符 &、"、'、<、>,且不会双重编码实体。
  • 参数:$text(必需,要翻译的文本)和 $domain(可选,文本域,默认为 'default')。
  • 与 esc_attr__() 的区别:esc_attr_e() 直接输出,而 esc_attr__() 返回字符串供 PHP 使用。
  • 内部实现:调用 esc_attr( translate( $text, $domain ) ) 进行转义和翻译。

代码示例

// 示例:在按钮的 value 属性中使用 esc_attr_e()
<input type="submit" value="<?php esc_attr_e( 'Submit', 'text-domain' ); ?>" />

注意事项

  • 仅用于输出到 HTML 属性中,确保安全性;如需在 PHP 代码中获取值,应使用 esc_attr__()。
  • 避免在非属性上下文中使用,以免不必要的转义影响输出。

📄 原文内容

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__() .

Parameters

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

Source

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

Changelog

Version Description
2.8.0 Introduced.

User Contributed Notes