Filter string like a slug

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
}

Leave a Comment