I work for an architecture company and our project names mostly go like this: house|something, bridge|somewhere, building|whatever.

Now, when I want to add a new project named like that, WordPress automatically converts it to housesomething, bridgesomewhere and puts that as the slug. I'd much prefer to keep some kind of separator, e.g. house-something, bridge-somewhere instead.

So, how to make WordPress convert | to - and not Empty String ("")? I'm obviously tired of doing that manually all the time.

It seems to me that it's very simple to do. It takes just a simple search and replace kind of thing if one knows where to look (in the WP core or wherever), but I haven't the slightest idea where to look, or what code to execute.


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 );
  • 1
    Thank you very much for your suggestion. It indeed is correct, however there was one small mistake which I fixed and edited your post to make it right. (str_replace() doesn't change the subject string, it's output needs to be returned.) Up to now I haven't really looked too much into the WP Core, but I've gone through the manual for the mentioned functions and can say this practical example elucidated a lot for me. Now I actually understand how this solution works. Thanks again and bye :)
    – Marg9
    May 17 '19 at 11:30
  • Glad to help. Thank you for correcting the error.
    – cjbj
    May 17 '19 at 12:04
  • Understanding actions and filters is probably the first thing to dive into when trying to understand core. I wrote a small tutorial for that: wordpress.stackexchange.com/questions/265952/…
    – cjbj
    May 17 '19 at 12:26

If you put spaces in between the words and the separator | the permalink will automatically include dashes between the words. For instance try this as your post title:

house | something, bridge | somewhere

That results in the slug:

  • Thanks for your post but this isn't really the solution as it requires that I change the titles, which is simply not how the company names it's projects, i.e. there shouldn't be any spaces.
    – Marg9
    May 17 '19 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.