Sanitize slug title

i need to convert this strings into the post url:

(numbers with the point and € symbol) ex: 9.000€ -> 9000-euro

Read More

I found this function but is not working:

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 = str_replace( '€', 'euro', $friendlyURL );
    $friendlyURL = str_replace( '€', 'euro', $friendlyURL );
    $friendlyURL = str_replace( '€', 'euro', $friendlyURL );
    $friendlyURL = trim($friendlyURL, '-');
    $friendlyURL = strtolower($friendlyURL);
    return $friendlyURL;

}

Thanks! 😉

Related posts

Leave a Reply

1 comment

  1. If you a look at the source you’ll see it’s because the sanitize_title filter fires after remove_accents() is called by sanitize_title().

    It does however, pass the raw title as the second argument so you can perform any substitutions on the raw title, apply remove_accents() and return it:

    function wpsesanitize_title_extra( $title, $raw_title, $context ) {
    
        $new_title = $raw_title;
    
        $new_title = str_replace( '€', 'euro', $new_title );
        $new_title = str_replace( '€', 'euro', $new_title );
        $new_title = str_replace( '€', 'euro', $new_title );
    
        if( 'save' == $context ){
            $new_title = remove_accents( $new_title );
        }
    
        return $new_title;
    
    }
    add_filter( 'sanitize_title', 'wpsesanitize_title_extra', 5, 3 );