Accents in wordpress permalinks

Several weeks ago i had the tough task to find or code a plugin that will let us keep swedish accents in wordpress permalinks. I only found one plugin that does this in the WordPress Plugin Directory, and of course it was not working… I assume it was written for some old version of wordpress. Anyways I started going through some function documentation at the codex and came up with quite simple solution, which will let you keep your language accents in wordpress permalinks.

Use a filter to replace the accents in wordpress permalinks with their UTF8 hex equivalent

I only had to find the proper filter and hook my function there. It worked perfectly below is the code of a MU Plugin for Swedish accents in wordpress permalinks.

Installation:

1) Create a folder named mu-plugins in wp-content and drop the file there. That is pretty much it. The solution will work for all the blogs in your network(if you have a Multi-Site installation)

The code is pretty straight forward but if you have any questions feel free to ask.

IMPORTANT Keep you files UTFed unless you want your editor to strip the characters and mess up your permalinks afterwards. Also keep in mind that this plugin will work only for your new permalinks. Old permalinks will remain intact(you can change them manually if needed).

The example below is for swedish accents in wordpress permalinks and is downloadable as MU-plugin and as a regular plugin as well.

I`m working on uploading it in the WordPress Plugin Directory

<?php
remove_filter( 'sanitize_title', 'sanitize_title_with_dashes');
add_filter( 'sanitize_title', 'restore_raw_title', 9, 3 );
function sweURLtoCHAR($text)
{
$url=array(
"%c3%85","%c3%a5",
"%c3%84","%c3%a4",
"%c3%96","%c3%b6",

);
$char=array(
 "Å","å",
 "Ä","ä",
 "Ö","ö",
);

$str = str_replace($char,$url,$text);
$str_new = str_replace(&quot; &quot;, &quot;&quot;, $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(&quot; &quot;, &quot;&quot;, $title);
return strtolower($title_new);
}
}
?>

In order to use this code snipplet as a regular plugin add the code below at the beginning of your php file

<?php
/*
Plugin Name: Swedish accents in permalinks
Plugin URI: http://awordpress.net/blog/accents-in-wordpress-permalinks/
Description: Allows Swedish accented characters in permalinks
Version: 2.0
Author: Pavel (aka Immortal) Petrov
Author URI: http://www.webbamboo.net
License: GPL2
*/
?>

You can read more about the WordPress standards of creating plugins in the Codex of course.

PS. If you code this use UTF8 without BOM, because otherwise you`ll get “Warning: Cannot modify header information – headers already sent by…”

Be the first to comment

Leave a Reply

Your email address will not be published.


*