wp_encode_emoji

The timeline below displays how wordpress function wp_encode_emoji has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Converts emoji characters to their equivalent HTML entity.
 *
 * This allows us to store emoji in a DB using the utf8 character set.
 *
 * @since 4.2.0
 *
 * @param string $content The content to encode.
 * @return string The encoded content.
 */
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;
}

WordPress Version: 6.1

/**
 * Converts emoji characters to their equivalent HTML entity.
 *
 * This allows us to store emoji in a DB using the utf8 character set.
 *
 * @since 4.2.0
 *
 * @param string $content The content to encode.
 * @return string The encoded content.
 */
function wp_encode_emoji($content)
{
    $emoji = _wp_emoji_list('partials');
    foreach ($emoji as $emojum) {
        $emoji_char = html_entity_decode($emojum);
        if (false !== strpos($content, $emoji_char)) {
            $content = preg_replace("/{$emoji_char}/", $emojum, $content);
        }
    }
    return $content;
}

WordPress Version: 5.3

/**
 * Convert emoji characters to their equivalent HTML entity.
 *
 * This allows us to store emoji in a DB using the utf8 character set.
 *
 * @since 4.2.0
 *
 * @param string $content The content to encode.
 * @return string The encoded content.
 */
function wp_encode_emoji($content)
{
    $emoji = _wp_emoji_list('partials');
    foreach ($emoji as $emojum) {
        $emoji_char = html_entity_decode($emojum);
        if (false !== strpos($content, $emoji_char)) {
            $content = preg_replace("/{$emoji_char}/", $emojum, $content);
        }
    }
    return $content;
}

WordPress Version: 5.1

/**
 * Convert emoji characters to their equivalent HTML entity.
 *
 * This allows us to store emoji in a DB using the utf8 character set.
 *
 * @since 4.2.0
 *
 * @param string $content The content to encode.
 * @return string The encoded content.
 */
function wp_encode_emoji($content)
{
    $emoji = _wp_emoji_list('partials');
    $compat = version_compare(phpversion(), '5.4', '<');
    foreach ($emoji as $emojum) {
        if ($compat) {
            $emoji_char = html_entity_decode($emojum, ENT_COMPAT, 'UTF-8');
        } else {
            $emoji_char = html_entity_decode($emojum);
        }
        if (false !== strpos($content, $emoji_char)) {
            $content = preg_replace("/{$emoji_char}/", $emojum, $content);
        }
    }
    return $content;
}

WordPress Version: 4.9

/**
 * Convert emoji characters to their equivalent HTML entity.
 *
 * This allows us to store emoji in a DB using the utf8 character set.
 *
 * @since 4.2.0
 *
 * @param string $content The content to encode.
 * @return string The encoded content.
 */
function wp_encode_emoji($content)
{
    $emoji = _wp_emoji_list('partials');
    foreach ($emoji as $emojum) {
        if (version_compare(phpversion(), '5.4', '<')) {
            $emoji_char = html_entity_decode($emojum, ENT_COMPAT, 'UTF-8');
        } else {
            $emoji_char = html_entity_decode($emojum);
        }
        if (false !== strpos($content, $emoji_char)) {
            $content = preg_replace("/{$emoji_char}/", $emojum, $content);
        }
    }
    return $content;
}

WordPress Version: 4.2

/**
 * Convert any 4 byte emoji in a string to their equivalent HTML entity.
 *
 * Currently, only Unicode 7 emoji are supported. Skin tone modifiers are allowed,
 * all other Unicode 8 emoji will be added when the spec is finalised.
 *
 * This allows us to store emoji in a DB using the utf8 character set.
 *
 * @since 4.2.0
 *
 * @param string $content The content to encode.
 * @return string The encoded content.
 */
function wp_encode_emoji($content)
{
    if (function_exists('mb_convert_encoding')) {
        $regex = '/(
		     \x23\xE2\x83\xA3               # Digits
		     [\x30-\x39]\xE2\x83\xA3
		   | \xF0\x9F[\x85-\x88][\xA6-\xBF] # Enclosed characters
		   | \xF0\x9F[\x8C-\x97][\x80-\xBF] # Misc
		   | \xF0\x9F\x98[\x80-\xBF]        # Smilies
		   | \xF0\x9F\x99[\x80-\x8F]
		   | \xF0\x9F\x9A[\x80-\xBF]        # Transport and map symbols
		)/x';
        $matches = array();
        if (preg_match_all($regex, $content, $matches)) {
            if (!empty($matches[1])) {
                foreach ($matches[1] as $emoji) {
                    /*
                     * UTF-32's hex encoding is the same as HTML's hex encoding.
                     * So, by converting the emoji from UTF-8 to UTF-32, we magically
                     * get the correct hex encoding.
                     */
                    $unpacked = unpack('H*', mb_convert_encoding($emoji, 'UTF-32', 'UTF-8'));
                    if (isset($unpacked[1])) {
                        $entity = '&#x' . ltrim($unpacked[1], '0') . ';';
                        $content = str_replace($emoji, $entity, $content);
                    }
                }
            }
        }
    }
    return $content;
}