Limiting the number of words or characters in WordPress slug/permalink. I would like to limit the number to the first 5 words (or 20 characters) even if the post title is longer.
example:
Title: Welcome to my site – this is my first post
url: mysite.com/welcome-to-my-site
Some function or alteration in the core WordPress to achieve this result?
You can probably do something with the
sanitize_title
hook based on the context conditionally, but I’m not familiar enough with where elsesanitize_title
is used to say for sure that this is a good solution. The trick to this is going to be limiting your slug without including stupid words that are going to hurt your SEO. As a launching point, try this:Note that that is completely untested and I literally just wrote it, so it may have some kinks in it, but it’s a good starting place for you.