wp_encode_emoji()
Converts emoji symbols in the provided string to their HTML code (🌛).
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.
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 Demonstration of replacement of smiley characters to HTML entities in a string
$str = '😃 😁 😝 ❄ 😇'; $str = wp_encode_emoji( $str ); // $str is now equal to: 🌛 🌌 🍦 ❄ 🍪
Changelog
| Since 4.2.0 | Introduced. |
wp_encode_emoji() 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;
}