wppaste
WordPress

wp_staticize_emoji( string $text ): string

Since
4.2.0
Source
wp-includes/formatting.php:6037
Converts emoji to a static img element.

Compatibility

WordPress
since 4.2.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$textstring
The content to encode.

Return value

string
The encoded content.

Performance profile

How much work a call to wp_staticize_emoji() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
9–54

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 120.

Plugin surface
2 hooks

Third-party callbacks on 'emoji_url', 'emoji_ext' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • regexregular expression over the whole inputpreg_split()called directly

What one call costs · 6 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_staticize_emoji() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!$text9–11mb_check_encoding()
$text12–13_wp_emoji_list()
!mb_check_encoding() && $encoded_text === false17mb_check_encoding(), wp_encode_emoji()
!mb_check_encoding() && $encoded_text !== false26–27mb_check_encoding(), wp_encode_emoji(), _wp_emoji_list()
$text && !$i39–40_wp_emoji_list(), apply_filters(), apply_filters(), preg_split()
!mb_check_encoding() && $encoded_text !== false && !$i53–54mb_check_encoding(), wp_encode_emoji(), _wp_emoji_list(), apply_filters(), apply_filters(), preg_split()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev12410–58214 more instructions than PHP 8.5
8.51209–5420
8.41209–542027 fewer instructions than PHP 8.3
8.314712–6320
8.214712–6320
8.114712–6320
7.414712–6320

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while wp_staticize_emoji() runs, in this order:

  1. apply_filters( emoji_url )filterline 6067 (+30 into the body)

    Filters the URL where emoji png images are hosted.

  2. apply_filters( emoji_ext )filterline 6070 (+33 into the body)

    Filters the extension of the emoji png files.

Uses · 4

Used by · 1

Source code

function wp_staticize_emoji( $text ) {	if ( ! str_contains( $text, '&#x' ) ) {		if ( ( function_exists( 'mb_check_encoding' ) && mb_check_encoding( $text, 'ASCII' ) ) || ! preg_match( '/[^\x00-\x7F]/', $text ) ) {			// The text doesn't contain anything that might be emoji, so we can return early.			return $text;		} else {			$encoded_text = wp_encode_emoji( $text );			if ( $encoded_text === $text ) {				return $encoded_text;			} 			$text = $encoded_text;		}	} 	$emoji = _wp_emoji_list( 'entities' ); 	// Quickly narrow down the list of emoji that might be in the text and need replacing.	$possible_emoji = array();	foreach ( $emoji as $emojum ) {		if ( str_contains( $text, $emojum ) ) {			$possible_emoji[ $emojum ] = html_entity_decode( $emojum );		}	} 	if ( ! $possible_emoji ) {		return $text;	} 	/** This filter is documented in wp-includes/formatting.php */	$cdn_url = apply_filters( 'emoji_url', 'https://s.w.org/images/core/emoji/17.0.2/72x72/' ); 	/** This filter is documented in wp-includes/formatting.php */	$ext = apply_filters( 'emoji_ext', '.png' ); 	$output = '';	/*	 * HTML loop taken from smiley function, which was taken from texturize function.	 * It'll never be consolidated.	 *	 * First, capture the tags as well as in between.	 */	$textarr = preg_split( '/(<.*>)/U', $text, -1, PREG_SPLIT_DELIM_CAPTURE );	$stop    = count( $textarr ); 	// Ignore processing of specific tags.	$tags_to_ignore       = 'code|pre|style|script|textarea';	$ignore_block_element = ''; 	for ( $i = 0; $i < $stop; $i++ ) {		$content = $textarr[ $i ]; 		// If we're in an ignore block, wait until we find its closing tag.		if ( '' === $ignore_block_element && preg_match( '/^<(' . $tags_to_ignore . ')>/', $content, $matches ) ) {			$ignore_block_element = $matches[1];		} 		// If it's not a tag and not in ignore block.		if ( '' === $ignore_block_element && strlen( $content ) > 0 && '<' !== $content[0] && str_contains( $content, '&#x' ) ) {			foreach ( $possible_emoji as $emojum => $emoji_char ) {				if ( ! str_contains( $content, $emojum ) ) {					continue;				} 				$file = str_replace( ';&#x', '-', $emojum );				$file = str_replace( array( '&#x', ';' ), '', $file ); 				$entity = sprintf( '<img src="%s" alt="%s" class="wp-smiley" style="height: 1em; max-height: 1em;" />', $cdn_url . $file . $ext, $emoji_char ); 				$content = str_replace( $emojum, $entity, $content );			}		} 		// Did we exit ignore block?		if ( '' !== $ignore_block_element && '</' . $ignore_block_element . '>' === $content ) {			$ignore_block_element = '';		} 		$output .= $content;	}

Changelog

Introduced in 4.2.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/formatting.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.