Last active 8 months ago

translate_slug_umlauts.php Raw
1add_filter( 'sanitize_title', '_translate_slug_umlauts', 5, 3 );
2/**
3* Transliterates German umlauts in post slugs for both the classic and block editors.
4*
5* @param string $title The sanitized title.
6* @param string $raw_title The title prior to any sanitization.
7* @param string $context The context in which the sanitization is occurring.
8*
9* @return string The modified title with German umlauts transliterated.
10*/
11function _translate_slug_umlauts( $title, $raw_title = null, $context = 'query' ) {
12// Hacky hook due to hacky core, see: http://core.trac.wordpress.org/ticket/16905
13if ( $raw_title !== null ) {
14$title = $raw_title; // undo remove_accents
15}
16
17// Replace umlauts and special characters with their transliterated equivalents.
18$umlaut_replace = [
19'Ä' => 'ae', 'ä' => 'ae',
20'Ö' => 'oe', 'ö' => 'oe',
21'Ü' => 'ue', 'ü' => 'ue',
22'ẞ' => 'ss', 'ß' => 'ss'
23];
24$title = strtr( $title, $umlaut_replace );
25
26if ( $context === 'save' ) {
27$title = remove_accents( $title ); // redo remove_accents
28}
29
30return $title;
31}