Converts emoji characters to their equivalent HTML entity.
Description
This allows us to store emoji in a DB using the utf8 character set.
Parameters
$content
stringrequired- The content to encode.
Source
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;
}
Changelog
Version | Description |
---|---|
4.2.0 | Introduced. |
You can also pass text with emoji.
You will not see any difference in output even after you use
wp_encode_emoji()
. Because the browser will still display the html encoded version as emoji. To see the difference, you need to View page source.To convert the html encoded version back to the unicode emoji, use
html_entity_decode()
.