wp_encode_emoji()WP 4.2.0

Converts emoji symbols in the provided string to their HTML code (&#x1f31b).

Emoji are commonly accepted (having a specification and supported by many devices) smiley images in the utf-8mb4 encoding, introduced in WordPress since version 4.2. For example: 🚀 🚚 ❄.

As of April 2015, not all smileys are supported, and a special script is used to display unrecognized emojis in the browser.

Such smiley symbols require special encoding utf8mb4 when placing them in the database; otherwise, the symbol will not be recognized and will not be saved correctly. Conversion using wp_encode_emoji() allows saving these symbols in an unsuitable encoding utf8, where they are saved not as symbols, but as their corresponding code.

The utf-8 encoding itself supports some of the emoji icons, but not all.

1 time — 0.00028 sec (fast) | 50000 times — 8.13 sec (fast)

No Hooks.

Returns

String. A string in which smiley symbols are replaced with their corresponding HTML entities.

Usage

wp_encode_emoji( $content );
$content(string) (required)
A string in which the symbols need to be replaced.

Examples

1

#1 Demonstration of replacement of smiley characters to HTML entities in a string

$str = '😃 😁 😝 ❄ 😇';

$str = wp_encode_emoji( $str );

// $str is now equal to: 🌛 🌌 🍦 ❄ &#x1f36a

Changelog

Since 4.2.0 Introduced.

wp_encode_emoji() code WP 6.9

function wp_encode_emoji( $content ) {
	$emoji = _wp_emoji_list( 'partials' );

	foreach ( $emoji as $emojum ) {
		$emoji_char = html_entity_decode( $emojum );
		if ( str_contains( $content, $emoji_char ) ) {
			$content = preg_replace( "/$emoji_char/", $emojum, $content );
		}
	}

	return $content;
}