0

By default, wordpress remove accents and weird characters to generate post_name in wp_posts table. This post_name is used when we change the permalink structure to /%postname%/ or some similar structures. In my language, i don't want to remove and replace many characters as it distorts the meaning.

I have change the sanitize_title_with_dashes function in wp-includes/formatting to:

function sanitize_title_with_dashes($title) {

$title = strip_tags($title);`
// Preserve escaped octets.
$title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
// Remove percent signs that are not part of an octet.
$title = str_replace('%', '', $title);
// Restore octets.
$title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);

/* comment out this code block

if (seems_utf8($title)) {
    if (function_exists('mb_strtolower')) {
        $title = mb_strtolower($title, 'UTF-8');
    }
    $title = utf8_uri_encode($title, 200);
}

$title = strtolower($title);

*/

$title = preg_replace('/&.+?;/', '', $title); // kill entities
$title = str_replace('.', '-', $title);

// comment out $title = preg_replace('/[^%a-z0-9 _-]/', '', $title);

$title = preg_replace('/\s+/', '-', $title);
$title = preg_replace('|-+|', '-', $title);
$title = trim($title, '-');

return $title;

}

The post_name is correctly generated for all characters in my language, for example http://1.todaytravels.info in the latest post, the url is very good, but wordpress simply does not find the post (it says not found). I guess i need to change something in permalink structure so that WP query to the correct post.

Anyone know how to resolve this problem?

Thanks for any help!

2 Answers 2

0

Why did you want to alter the URI here? SEO optimization?

An URI has a limited number of allowed chars, defined in an RFC. That's the reason WordPress uses utf8_uri_encode to generate a permalink based on the posttitle.

You can find more information in this stackoverflow question: Which characters make a URL invalid?

EDIT

I've checked the wp_insert_post() function in wp-includes/post.php, which generates a new post in the database based on the submitted $_POST array. There you have a call to sanitize_post() on line 2487 and also a call to sanitize_title() on line 2548. You should check both functions.

Another option could be, that an attached filter to one of these sanitize functions are fired in the background. You could remove any filters with the function remove_all_filters()

For example:

remove_all_filters('sanitize_title', 99);

6
  • Ya, i want to keep all valid characters untouched in the title. These are valid language characters, it is definitely valid for url (i see many websites used that too). I want not to use the utf8_uri_encode, but maybe some more changes are needed so that WP query to the correct post
    – Pam Apple
    Commented Aug 11, 2012 at 9:01
  • Maybe utf8_uri_encode is also used in another rewrite rule function. That function direct wordress to post content, but i'm not sure where to find it. Do you have any idea?
    – Pam Apple
    Commented Aug 11, 2012 at 9:03
  • @PamApple I've edited my answer. Maybe this brings you a bit further.
    – Roman
    Commented Aug 11, 2012 at 9:29
  • Hi Rofflox, in the sanitize_title() function (located in formatting.php), i commented out the line remove_accents because this changes the characters. After doing this, i can open my posts and firefox browser indicates correct characters, but in the page source (and post_name in wp_posts table) there are a lot of junks (% ss characters). Then i comment out the utf8_uri_encode, the post_name field now show correct characters, but i can no longer open the post (Not Found). I can't find any relation of the problem with sanitize_post because the post does not relate to the url.
    – Pam Apple
    Commented Aug 11, 2012 at 10:12
  • I guess it means that sanitize_title is not the culprit of the not found problem. The problem is in utf8_uri_encode that need to be comment out somewhere else. I'm not sure any other function also use utf8_uri_encode
    – Pam Apple
    Commented Aug 11, 2012 at 10:15
1

Non-ASCII characters in URLs have to be encoded. Use rawurlencode() to do that.

Example: a slug öüä has to be encoded as %C3%B6%C3%BC%C3%A4.
You can still enter öüä into the address bar, the browser will send the encoded URL to the server.
$_SERVER['REQUEST_URI'] will be 7%C3%B6%C3%BC%C3%A4, $_SERVER['REDIRECT_URL'] will be öüä.

enter image description here

Do not edit WordPress files, use a filter instead. You cannot run updates anymore otherwise.
In my plugin Germanix I hook into 'sanitize_title', take the second argument (the raw title) and change that. You should do the same.

2
  • Hi toscho, your plugin does not work for me because it removes non-english character. Or maybe there is something i need to change. I have resolved my problem. Thanks!
    – Pam Apple
    Commented Aug 11, 2012 at 11:27
  • Of course it doesn’t. :) It just shows you how to hook into WordPress without changing a core file.
    – fuxia
    Commented Aug 11, 2012 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.