Search code examples
phpwordpressurl-rewritingslug

Wordpress Slug Umlaut


I have 2 problems related to each other!

How do I get slugs working for pretty URLs like : ä , ü , ö , â, á, or what ever exists there.

I dont want them to rewrite it! I really want to make it work with that.. The only way I found was an outdated plugin called 123Echt Laut,.. it still works, but I prefer to have something up-to-date, or a php code that makes it work without rewriting it from, for example ä to ae! I want ä to be displayed in the url as ä!

Plus I want to retrieve that slug.. what I found with google was this:

?php global $post;
$post_slug=$post->post_name;?>

I wanted it to retrieve the word: fünkel What I got was : f%c3%bcnkel


Solution

  • This is probably a duplicate of this post although that one is on WordPress Stack Exchange. In fact, this question should be posted there as well since it is technically a WordPress question. That being said, here's the answer posted there which I think is what you are looking for.

    remove_filter( 'sanitize_title', 'sanitize_title_with_dashes');
    add_filter( 'sanitize_title', 'restore_raw_title', 9, 3 );
    function sweURLtoCHAR($text)
    {
      $url=array(
        "%C3%84","%C3%A4",
        "%C3%9C","%C3%BC",
        "%C3%96","%C3%B6",
        "%C3%82","%C3%A2",
        "%C3%81","%C3%A1"
      );
      $char=array(
         "Ä","ä",
         "Ü","ü",
         "Ö","ö",
         "Â","â",
         "Á","á"
      );
      $str = str_replace($char,$url,$text);
      $str_new = str_replace(" ", "", $str);
      return strtolower($str_new);
    }
    function restore_raw_title( $title, $raw_title, $context ) {
      if ( $context == 'save' )
       return sweURLtoCHAR($raw_title);
      else {
       $title_new = str_replace(" ", "", $title);
       return strtolower($title_new);
      }
    }
    

    Edit: Oh, and to get "fünkel" from "f%c3%bcnkel", use urldecode(). And for that matter, come to think of it, you might be able to replace that sweURLtoCHAR function with something like this (untested):

    function sweURLtoCHAR($text)
    {
      $str = urldecode($text);
      $str_new = str_replace(" ", "", $str);
      return strtolower($str_new);
    }