I have a WordPress site using pretty permalinks. I want to filter a string to that the it ends up with the exact same formatting as the url slug, does anyone know the filter or function to do so?

1 Answer 1


You can use sanitize_title() function:

$string = "This is title string";
// return "this-is-title-string"
$slug   = sanitize_title( $string );

You can also filter the result of sanitize_title() function using sanitize_title filter:

add_filter( 'sanitize_title' , 'sanitize_filter_callback', 10, 3 );
function sanitize_filter_callback( $title, $raw_title, $context ) {
    // do something

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.