wp_encode_emoji()WP 4.2.0

Convert emoji characters to their equivalent HTML entity.

This allows us to store emoji in a DB using the utf8 character set.

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

No Hooks.

Return

String. The encoded content.

Usage

wp_encode_emoji( $content );
$content(string) (required)
The content to encode.

Examples

0

#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.5.2

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;
}