translate_smiley( array $matches ): string
- Since
- 2.8.0
- Source
wp-includes/formatting.php:3437
Description
Callback handler for convert_smilies().
Looks up one smiley code in the $wpsmiliestrans global array and returns an <img> string for that smiley.
Compatibility
- WordPress
- since 2.8.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
$matchesarray- Single match. Smiley code to convert to image.
Return value
string- Image string for smiley.
Performance profile
How much work a call to translate_smiley() 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
- Trivial
- Scaling
- Constant
- Instructions
- 6–51
- Plugin surface
- 1 hook
- Called by
- 0
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 54.
Third-party callbacks on 'smilies_src' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_filters()called directly
Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 5 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost translate_smiley() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6 | none |
!preg_match() && !$ext | 22 | reset(), preg_match() |
preg_match() && !$ext | 27 | reset(), preg_match(), strtolower() |
!preg_match() && $ext | 46 | reset(), preg_match(), includes_url(), site_url(), apply_filters(), esc_url(), esc_attr() |
preg_match() && $ext | 51 | reset(), preg_match(), strtolower(), includes_url(), site_url(), apply_filters(), esc_url(), esc_attr() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 53 | 6–50 | 3 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 54 | 6–51 | 3 | |
| 8.4 | 54 | 6–51 | 3 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 57 | 6–54 | 3 | |
| 8.2 | 57 | 6–54 | 3 | |
| 8.1 | 57 | 6–54 | 3 | |
| 7.4 | 57 | 6–54 | 3 |
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 · 1
One hook fires while translate_smiley() runs, in this order:
- apply_filters( smilies_src )filterline 3465 (+28 into the body)
Filters the Smiley image URL before it's used in the image element.
Uses · 5
- apply_filters()Calls the callback functions that have been added to a filter hook.
- includes_url()Retrieves the URL to the includes directory.
- site_url()Retrieves the URL for the current site where WordPress application files (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible.
- esc_url()Checks and cleans a URL.
- esc_attr()Escaping for HTML attributes.
Source code
function translate_smiley( $matches ) { global $wpsmiliestrans; if ( 0 === count( $matches ) ) { return ''; } $smiley = trim( reset( $matches ) ); $img = $wpsmiliestrans[ $smiley ]; $matches = array(); $ext = preg_match( '/\.([^.]+)$/', $img, $matches ) ? strtolower( $matches[1] ) : false; $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp', 'avif' ); // Don't convert smilies that aren't images - they're probably emoji. if ( ! in_array( $ext, $image_exts, true ) ) { return $img; } /** * Filters the Smiley image URL before it's used in the image element. * * @since 2.9.0 * * @param string $smiley_url URL for the smiley image. * @param string $img Filename for the smiley image. * @param string $site_url Site URL, as returned by site_url(). */ $src_url = apply_filters( 'smilies_src', includes_url( "images/smilies/$img" ), $img, site_url() ); return sprintf( '<img src="%s" alt="%s" class="wp-smiley" style="height: 1em; max-height: 1em;" />', esc_url( $src_url ), esc_attr( $smiley ) );}Changelog
Introduced in 2.8.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 7.0.4 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.