Advertisement
mbis

Remove Emoji from WordPress slugs

Sep 28th, 2020 (edited)
1,589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function pm_remove_emojis_from_slugs( $str ) {
  2.     // Replace hexadecimal sequences with their corresponding Unicode characters
  3.     $str = preg_replace_callback( '/x([0-9a-fA-F]{4,6})/i', function ( $matches ) {
  4.         // Convert the hex to decimal, then to the corresponding Unicode character
  5.         return mb_convert_encoding( pack( 'H*', $matches[1] ), 'UTF-8', 'UCS-4BE' );
  6.     }, $str );
  7.  
  8.     // Remove remaining emojis using a regular expression
  9.     $str = preg_replace( '/[\x{1F600}-\x{1F64F}\x{1F300}-\x{1F5FF}\x{1F900}-\x{1F9FF}\x{1F1E0}-\x{1F1FF}]/u', '', $str );
  10.  
  11.     // Remove widow & duplicated slashes
  12.     $clean = preg_replace( '|-+|', '-', $str );
  13.     $clean = preg_replace( '/([-]*[\/]+[-]*)/', '/', $clean );
  14.     $clean = preg_replace( '/([\/]+)/', '/', $clean );
  15.  
  16.     // Trim slashes, dashes and whitespaces
  17.     return trim( $clean, " /-" );
  18. }
  19. add_filter('wp_unique_post_slug', 'pm_remove_emojis_from_slugs', 100);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement