SHOW:
|
|
- or go back to the newest paste.
1 | - | function pm_remove_emojis($regex) { |
1 | + | function pm_remove_emojis_from_slugs( $str ) { |
2 | - | $regex = str_replace('[^', '(%f0%9f%[\w]{2}%[\w]{2}|[^', $regex); |
2 | + | // Replace hexadecimal sequences with their corresponding Unicode characters |
3 | - | $regex = str_replace(']/ui', '])/ui', $regex); |
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 $regex; |
5 | + | return mb_convert_encoding( pack( 'H*', $matches[1] ), 'UTF-8', 'UCS-4BE' ); |
6 | }, $str ); | |
7 | - | add_filter('permalink_manager_sanitize_regex', 'pm_remove_emojis', 1); |
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); |