How to remove certain words from url slug

There should be no problem with unset. There is your answer. What matters with a filter is what you return and unseting those keys before your rebuild the string and return it, works.

You are doing too much processing by explodeing your $keys_false inside a loop, and you have a typo, and you are better off just making an array instead of creating a string that you convert to an array, and PHP has a function to do what your loop is doing. I reorganized it for you.

function remove_false_words($slug) {
  if (!is_admin()) return $slug;
  $keys_false = array('a','and','above','the','also');
  $slug = explode ('-', $slug);
  $slug = array_diff($slug,$keys_false);
  return implode ('-', $slug);
}
var_dump(remove_false_words('a-boy-and-his-dog')); // proof of concept

Leave a Comment