0

I want to remove some characters from the slug, like "¿" that we use in spanish for opening a question.

If I go to wp-includes/formatting.php and in

function sanitize_title_with_dashes($title)

line 820 wp32 I add this

$title = str_replace('¿', '', $title);

just before $title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);

And it is working correctly. So when I write in the title "¿Qué tal?" the slug is "que-tal"

Now, how to do it with a function so I can use it via functions.php or via a plugin??

I am doing this but it is not working

function limpiar_slug($slug)
{
    $slug = str_replace('¿', '', $slug);
    return $slug;
}

add_filter('sanitize_title_with_dashes', 'limpiar_slug');

Any suggestion???

Thanks in advance

1
  • You can't just pop filters onto functions, that's not how the filter system works, filters can only be attached to filter hooks and i'm quite sure sanitize_title_with_dashes is not a filter hook.
    – t31os
    Commented Jul 7, 2011 at 16:54

2 Answers 2

1

Following on from my comment on your question.

The solution would be to hook your own filter onto the same functions sanitize_title_with_dashes does, in this case i think you're just aiming to hook onto sanitize_title as is done with sanitize_title_with_dashes.

Simply ensure you hook on after sanitize_title_with_dashes, which is hooked on at the default priority of 10.

Eg.

add_filter('sanitize_title', 'limpiar_slug', 11 );

No need to hack core files.

EDIT BELOW:

You could give some regex a try, here's a sample you can play with, code adapted from Adriana Villafañe's code in response to Regular expression - any text to URL friendly one on StackOverflow.

add_filter( 'sanitize_title', 'sanitize_title_extra' );

function sanitize_title_extra( $title ) {

    $friendlyURL = htmlentities($title, ENT_COMPAT, "UTF-8", false); 
    $friendlyURL = preg_replace('/&([a-z]{1,2})(?:acute|lig|grave|ring|tilde|uml|cedil|caron);/i','\1',$friendlyURL);
    $friendlyURL = html_entity_decode($friendlyURL,ENT_COMPAT, "UTF-8"); 
    $friendlyURL = preg_replace('/[^a-z0-9-]+/i', '-', $friendlyURL);
    $friendlyURL = preg_replace('/-+/', '-', $friendlyURL);
    $friendlyURL = trim($friendlyURL, '-');
    $friendlyURL = strtolower($friendlyURL);
    return $friendlyURL;

}

Seems to do the trick of clearing up some unwanted characters, feel free to tweak, etc...

7
  • Your right, sanitize_title_with_dashes IS the actual filter I didn't thought of it that way, still it should be removing special characters by default Commented Jul 7, 2011 at 17:01
  • The problem has been acknowledged in this Trac Ticket.
    – t31os
    Commented Jul 7, 2011 at 17:18
  • Hi, I tried with that proposal, adding the 11 but still not working... BTW, there are more characters like copyright and registered, try to copy this in the title of your post: Copyright © 2008-2011 ¿ ¡ holaos®
    – antorome
    Commented Jul 7, 2011 at 17:45
  • Have you tried removing the default wordpress filter and then adding your own the way you described it on your question? Like remove_filter('sanitize_title', 'sanitize_title_with_dashes') and then adding your custom filter with the str_replace like add_filter('sanitize_title', 'limpiar_slug' ) Commented Jul 7, 2011 at 18:55
  • See update to answer.
    – t31os
    Commented Jul 7, 2011 at 20:02
0

The problem is that the function sanitize_title_with_dashes doesn't use filters (which is odd I think) so you won't be able to change it without hacking the formatting.php file directly :(

2
  • I have also tried with the function sanitize_title but doesnt work either. May be in another function??
    – antorome
    Commented Jul 7, 2011 at 16:54
  • I think the problem is the actual RegEx of the function, according to the functions documentation it "Limits the output to alphanumeric characters, underscore (_) and dash (-)" so in theory it should remove all special characters, maybe a bug? Commented Jul 7, 2011 at 16:56

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.