wp_staticize_emoji( string $text ): string
- Since
- 4.2.0
- Source
wp-includes/formatting.php:6001
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
- Scaling
- Scales with input
- Instructions
- 9–54
- Plugin surface
- 2 hooks
- Called by
- 1
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 120.
Third-party callbacks on 'emoji_url', 'emoji_ext' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - regexregular expression over the whole input
preg_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.
| When | Instructions | Calls it makes |
|---|---|---|
!$text | 9–11 | mb_check_encoding() |
$text | 12–13 | _wp_emoji_list() |
!mb_check_encoding() && $encoded_text === false | 17 | mb_check_encoding(), wp_encode_emoji() |
!mb_check_encoding() && $encoded_text !== false | 26–27 | mb_check_encoding(), wp_encode_emoji(), _wp_emoji_list() |
$text && !$i | 39–40 | _wp_emoji_list(), apply_filters(), apply_filters(), preg_split() |
!mb_check_encoding() && $encoded_text !== false && !$i | 53–54 | mb_check_encoding(), wp_encode_emoji(), _wp_emoji_list(), apply_filters(), apply_filters(), preg_split() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 124 | 10–58 | 21 | 4 more instructions than PHP 8.5 |
| 8.5 | 120 | 9–54 | 20 | |
| 8.4 | 120 | 9–54 | 20 | 27 fewer instructions than PHP 8.3 |
| 8.3 | 147 | 12–63 | 20 | |
| 8.2 | 147 | 12–63 | 20 | |
| 8.1 | 147 | 12–63 | 20 | |
| 7.4 | 147 | 12–63 | 20 |
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:
- apply_filters( emoji_url )filterline 6031 (+30 into the body)
Filters the URL where emoji png images are hosted.
- apply_filters( emoji_ext )filterline 6034 (+33 into the body)
Filters the extension of the emoji png files.
Uses · 4
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- wp_encode_emoji()Converts emoji characters to their equivalent HTML entity.
- _wp_emoji_list()Returns arrays of emoji data.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 1
- wp_staticize_emoji_for_email()Converts emoji in emails into static images.
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/16.0.1/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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.