Don’t replace “|” with Empty String (“”) when generating slugs from title

When WordPress inserts a post, it runs the title through a filter called sanitize_title to get the slug. By default there is a function called santize_title_with_dashes attached to this filter with priority 10. This function simply strips out the |. If it is surrounded by spaces those spaces will be converted to hyphens.

So your task is to run a filter on the same hook before (say, priority 9) the default one and replace the | with - before it gets stripped away. Like this:

add_filter( 'sanitize_title', function ( $title ) {
    return str_replace( '|', '-', $title );
}, 9 );

Leave a Comment