Is there a way to overwrite WP slug validation function to only accept basic characters ([A-Z0-9-]) as I want to avoid the long encoded permlink.
Thanks.
For now I used the sanitize_title
filter, and removed all unwanted characters, which forces my users to use only [a-z0-9-]
as a valid input for titles.
## Allow only [a-z0-9-] in the slug
function my_clear_title($title) {
$pattern = '/[^a-z_0-9- ]/i';
$replace_with = '';
return preg_replace($pattern, $replace_with, $title);
}
add_filter('sanitize_title', 'my_clear_title', 3, 1);
sanitize_title
has a context parameter which you might want to check to make sure you are not sanitizing things that are not slugs in this way.
Apr 21, 2016 at 15:37
context
parameter is more like a "when" and not "what" parameter and it default to save
. from my testing it looks like it works for permalinks as well on names for tags and category's (which is great)