0

Trying to create a SEO plugin, that will remove stop words from slugs.

This is how to explode the $slug into a list, and explode the variable containing the stop words :

add_filter ('sanitize title', 'remove_false_words');
function remove_false_words($slug) {
    if (!is_Admin()) return $slug;

    $slug = explode ('-', $slug);

    foreach ($slug as $key => $word) {
        //The $slug has been exploded and retrieves its value

        $keys_false = 'a,above,the,also';
        $keys = explode (',', $keys_false);

        foreach ($keys as $1 => $wordfalse) {
            if ($word == $wordfalse) {
                unset ($slug[$k]);
            }
        }
    }
    return implode ('-', $slug);
}

Is it correct to unset the value here? Or should I use a different way of achieving the same result?

  • $keys_false is a string right now. Is that right? Should be array I think – GhostToast Jun 6 '13 at 13:45
  • @GhostToast : mattnewbie explodes the string on the next line-- unnecessary step but it isn't broken. – s_ha_dum Jun 6 '13 at 13:47
  • Oh yes. I forget about explode() as I only ever find my self using it's evil twin, implode() – GhostToast Jun 6 '13 at 13:48
2

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
  • Thank-you. I have been looking as some wordpress snippets that I found on the internet, make need to re-think how I study. As this was one of the snippets I found, & obviously isn't very good. – mattnewbie Jun 6 '13 at 14:41
  • PHP suffers from being a "copy and paste and hack together without knowing what you are doing" language. There is a lot of a bad stuff out there. – s_ha_dum Jun 6 '13 at 14:43

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.