translate_slug_umlauts.php
· 1.0 KiB · PHP
Raw
add_filter( 'sanitize_title', '_translate_slug_umlauts', 5, 3 );
/**
* Transliterates German umlauts in post slugs for both the classic and block editors.
*
* @param string $title The sanitized title.
* @param string $raw_title The title prior to any sanitization.
* @param string $context The context in which the sanitization is occurring.
*
* @return string The modified title with German umlauts transliterated.
*/
function _translate_slug_umlauts( $title, $raw_title = null, $context = 'query' ) {
// Hacky hook due to hacky core, see: http://core.trac.wordpress.org/ticket/16905
if ( $raw_title !== null ) {
$title = $raw_title; // undo remove_accents
}
// Replace umlauts and special characters with their transliterated equivalents.
$umlaut_replace = [
'Ä' => 'ae', 'ä' => 'ae',
'Ö' => 'oe', 'ö' => 'oe',
'Ü' => 'ue', 'ü' => 'ue',
'ẞ' => 'ss', 'ß' => 'ss'
];
$title = strtr( $title, $umlaut_replace );
if ( $context === 'save' ) {
$title = remove_accents( $title ); // redo remove_accents
}
return $title;
}
| 1 | add_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 | */ |
| 11 | function _translate_slug_umlauts( $title, $raw_title = null, $context = 'query' ) { |
| 12 | // Hacky hook due to hacky core, see: http://core.trac.wordpress.org/ticket/16905 |
| 13 | if ( $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 | |
| 26 | if ( $context === 'save' ) { |
| 27 | $title = remove_accents( $title ); // redo remove_accents |
| 28 | } |
| 29 | |
| 30 | return $title; |
| 31 | } |