wp_staticize_emoji

Convert emoji to a static img element.

Description

(string) wp_staticize_emoji( (string) $text ); 

Returns (string)

The encoded content.

Parameters (1)

0. $text (string)
The content to encode.

Usage

  1. if ( !function_exists( 'wp_staticize_emoji' ) ) { 
  2. require_once ABSPATH . WPINC . '/formatting.php'; 
  3.  
  4. // The content to encode. 
  5. $text = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_staticize_emoji($text); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/formatting.php  
  1. function wp_staticize_emoji( $text ) { 
  2. $text = wp_encode_emoji( $text ); 
  3.  
  4. /** This filter is documented in wp-includes/formatting.php */ 
  5. $cdn_url = apply_filters( 'emoji_url', 'https://s.w.org/images/core/emoji/2.2.1/72x72/' ); 
  6.  
  7. /** This filter is documented in wp-includes/formatting.php */ 
  8. $ext = apply_filters( 'emoji_ext', '.png' ); 
  9.  
  10. $output = ''; 
  11. /** 
  12. * HTML loop taken from smiley function, which was taken from texturize function. 
  13. * It'll never be consolidated. 
  14. * First, capture the tags as well as in between. 
  15. */ 
  16. $textarr = preg_split( '/(<.*>)/U', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); 
  17. $stop = count( $textarr ); 
  18.  
  19. // Ignore processing of specific tags. 
  20. $tags_to_ignore = 'code|pre|style|script|textarea'; 
  21. $ignore_block_element = ''; 
  22.  
  23. for ( $i = 0; $i < $stop; $i++ ) { 
  24. $content = $textarr[$i]; 
  25.  
  26. // If we're in an ignore block, wait until we find its closing tag. 
  27. if ( '' == $ignore_block_element && preg_match( '/^<(' . $tags_to_ignore . ')>/', $content, $matches ) ) { 
  28. $ignore_block_element = $matches[1]; 
  29.  
  30. // If it's not a tag and not in ignore block. 
  31. if ( '' == $ignore_block_element && strlen( $content ) > 0 && '<' != $content[0] ) { 
  32. $matches = array(); 
  33. if ( preg_match_all( '/(DZ(e[6-9a-f]|f[0-9a-f]);) {2}/', $content, $matches ) ) { 
  34. if ( ! empty( $matches[0] ) ) { 
  35. foreach ( $matches[0] as $flag ) { 
  36. $chars = str_replace( array( '&#x', ';'), '', $flag ); 
  37.  
  38. list( $char1, $char2 ) = str_split( $chars, 5 ); 
  39. $entity = sprintf( '<img src="%s" alt="%s" class="wp-smiley" style="height: 1em; max-height: 1em;" />', $cdn_url . $char1 . '-' . $char2 . $ext, html_entity_decode( $flag ) ); 
  40.  
  41. $content = str_replace( $flag, $entity, $content ); 
  42.  
  43. // Loosely match the Emoji Unicode range. 
  44. $regex = '/(&#x[2-3][0-9a-f]{3};|[1-6][0-9a-f]{2};)/'; 
  45.  
  46. $matches = array(); 
  47. if ( preg_match_all( $regex, $content, $matches ) ) { 
  48. if ( ! empty( $matches[1] ) ) { 
  49. foreach ( $matches[1] as $emoji ) { 
  50. $char = str_replace( array( '&#x', ';'), '', $emoji ); 
  51. $entity = sprintf( '<img src="%s" alt="%s" class="wp-smiley" style="height: 1em; max-height: 1em;" />', $cdn_url . $char . $ext, html_entity_decode( $emoji ) ); 
  52.  
  53. $content = str_replace( $emoji, $entity, $content ); 
  54.  
  55. // Did we exit ignore block. 
  56. if ( '' != $ignore_block_element && '</' . $ignore_block_element . '>' == $content ) { 
  57. $ignore_block_element = ''; 
  58.  
  59. $output .= $content; 
  60.  
  61. return $output;