Wordpress - Is sanitize_title enough to generate post slugs?

You are almost there. The function you need is sanitize_title_with_dashes( $title )


sanitize_title() seems to be the only one you need.

In wp-includes/default-filters.php line 211 you will find:

add_filter( 'sanitize_title', 'sanitize_title_with_dashes', 10, 3);

This means that calling sanitize_title() will first remove all the special characters, then apply the sanitize_title filter, thus calling sanitize_title_with_dashes()

As @JHoffmann pointed out, simply calling sanitize_title_with_dashes() will not remove special characters.


Well, there is already an answer, but I wanted to expand it a bit, so here are my findings:

If we have a look in wp_insert_post() we see, the $post_name is sanitized using wp_sanitize_title() ( see wp-includes/post.php)

In the function sanitize_title() we have a filter sanitize_title. This is interesting, since in the default filters sanitize_title_with_dashes() is hooked into this filter (see wp-includes/default-filters.php).

<?php
echo sanitize_title( 'Â+ÄÖßáèäç' ) // aaeoessaeaec
?>

I tried sanitize_title() but it leaves %c2 %a0 in result.

This sounds strange. It would be great to know the input value, but following wp_insert_post() sanitize_title() seems to be enough.