wp_encode_emoji( string $content )

Convert emoji characters to their equivalent HTML entity.


Description Description

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


Top ↑

Parameters Parameters

$content

(string) (Required) The content to encode.


Top ↑

Return Return

(string) The encoded content.


Top ↑

Source Source

File: wp-includes/formatting.php

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


Top ↑

Changelog Changelog

Changelog
Version Description
4.2.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Mehbub Rashid

    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.

    print_r(' This is a text with emoji 🎃' ); // This is a text with emoji 🎃
    print_r( wp_encode_emoji( 'This is a text with emoji 🎃' ) ); // This is a text with emoji 🎃
    

    To convert the html encoded version back to the unicode emoji, use html_entity_decode().

    $html_encoded = wp_encode_emoji( '🎃' );
    $html_decoded = html_entity_decode( $html_encoded );
    

You must log in before being able to contribute a note or feedback.