函数文档

translate_smiley()

💡 云策文档标注

概述

translate_smiley() 是 WordPress 中用于将表情符号代码转换为对应图像 HTML 字符串的函数,作为 convert_smilies() 的回调处理程序。它通过全局数组 $wpsmiliestrans 查找表情代码,并生成包含过滤后的图像 URL 的 标签。

关键要点

  • 函数作用:将单个表情代码(如 :-))转换为图像 HTML 字符串,例如 smiley
  • 参数:接受一个数组 $matches,包含要转换的表情代码,必须为非空。
  • 返回值:返回一个字符串,表示表情图像的 HTML 代码,如果输入为空则返回空字符串。
  • 处理逻辑:检查表情对应的文件扩展名是否为图像格式(如 jpg、png),非图像文件(如 emoji)直接返回原字符串。
  • Hook:使用 apply_filters('smilies_src', ...) 过滤图像 URL,允许开发者自定义表情图像源。
  • 相关函数:依赖 includes_url()、site_url()、esc_url()、esc_attr() 等辅助函数构建安全的输出。

代码示例

function translate_smiley( $matches ) {
	global $wpsmiliestrans;

	if ( 0 === count( $matches ) ) {
		return '';
	}

	$smiley = trim( reset( $matches ) );
	$img    = $wpsmiliestrans[ $smiley ];

	$matches    = array();
	$ext        = preg_match( '/.([^.]+)$/', $img, $matches ) ? strtolower( $matches[1] ) : false;
	$image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp', 'avif' );

	// Don't convert smilies that aren't images - they're probably emoji.
	if ( ! in_array( $ext, $image_exts, true ) ) {
		return $img;
	}

	/**
	 * Filters the Smiley image URL before it's used in the image element.
	 *
	 * @since 2.9.0
	 *
	 * @param string $smiley_url URL for the smiley image.
	 * @param string $img        Filename for the smiley image.
	 * @param string $site_url   Site URL, as returned by site_url().
	 */
	$src_url = apply_filters( 'smilies_src', includes_url( "images/smilies/$img" ), $img, site_url() );

	return sprintf( '<img src="%s" alt="%s" />', esc_url( $src_url ), esc_attr( $smiley ) );
}

注意事项

  • 函数依赖于全局变量 $wpsmiliestrans,确保在调用前该数组已正确初始化包含表情映射。
  • 仅处理图像文件格式的表情,非图像(如文本 emoji)会原样返回,避免不必要的转换。
  • 使用 Hook 'smilies_src' 允许插件或主题修改表情图像的 URL,增强灵活性。
  • 输出经过 esc_url() 和 esc_attr() 转义,确保 HTML 安全性,防止 XSS 攻击。
  • 自 WordPress 2.8.0 版本引入,是核心表情处理功能的一部分。

📄 原文内容

Converts one smiley code to the icon graphic file equivalent.

Description

Callback handler for convert_smilies() .

Looks up one smiley code in the $wpsmiliestrans global array and returns an <img> string for that smiley.

Parameters

$matchesarrayrequired
Single match. Smiley code to convert to image.

Return

string Image string for smiley.

Source

function translate_smiley( $matches ) {
	global $wpsmiliestrans;

	if ( 0 === count( $matches ) ) {
		return '';
	}

	$smiley = trim( reset( $matches ) );
	$img    = $wpsmiliestrans[ $smiley ];

	$matches    = array();
	$ext        = preg_match( '/.([^.]+)$/', $img, $matches ) ? strtolower( $matches[1] ) : false;
	$image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp', 'avif' );

	// Don't convert smilies that aren't images - they're probably emoji.
	if ( ! in_array( $ext, $image_exts, true ) ) {
		return $img;
	}

	/**
	 * Filters the Smiley image URL before it's used in the image element.
	 *
	 * @since 2.9.0
	 *
	 * @param string $smiley_url URL for the smiley image.
	 * @param string $img        Filename for the smiley image.
	 * @param string $site_url   Site URL, as returned by site_url().
	 */
	$src_url = apply_filters( 'smilies_src', includes_url( "images/smilies/$img" ), $img, site_url() );

	return sprintf( '<img src="%s" alt="%s" class="wp-smiley" style="height: 1em; max-height: 1em;" />', esc_url( $src_url ), esc_attr( $smiley ) );
}

Hooks

apply_filters( ‘smilies_src’, string $smiley_url, string $img, string $site_url )

Filters the Smiley image URL before it’s used in the image element.

Changelog

Version Description
2.8.0 Introduced.