Home / admin / Better SEO automatically remove stop words from slug
  • How to Better SEO automatically remove stop words from slug

    better-seo-automatically-remove-stop-words-from-slug

    Adding this snippet to the functions.php of your wordpress theme will automatically remove stop words from the slug for better SEO. This is an updated version of the (Better SEO)

    ( example screenshot ) This version was contributed by Alexandre M. Cajaty. I only added a small selection of stop words within $keys_false for an extensive list of stop words (download) 429 words. ( click code to copy )

    wordpress snippet : PHP

    <>
    add_filter('sanitize_title', 'remove_false_words');
    function remove_false_words($slug) {
        if (!is_admin()) return $slug;
        $slug = explode('-', $slug);
        foreach ($slug as $k => $word) {
    		//false words list separated for commas
    		$keys_false = 'a,about,above,across,after,again,against,all,almost,alone,along,already,also';
    		$keys = explode(',', $keys_false);
    		foreach ($keys as $l => $wordfalse) {
    			if ($word==$wordfalse) {
    				unset($slug[$k]);
    			}
    		}
        }
        return implode('-', $slug);
    }
    

    ( WordPress codex functions, hooks, in this snippet. )

    sanitize_title, add_filter, is_admin, wp,
  • I really like this, but I expect this would change current permalinks/URLs, correct? If that’s true, is there a way to make sure 404’s don’t start showing up?

    • This changes the text when you publish a post only. First add this snippet then create a new post with some of the stop words above when you save it you will notice the permalink will get the stopwords removed. This would not cause problems with existing pages.

      • Bob

        Do you think its a good idea to remove all 493 slug words from the wordpress slug?

        • That list is just to give you a starting point I found that online a while back and figured it would be a great list to include with the post. I would probably not just add the whole list but may add sections of it.

    • This changes the text when you publish a post only. First add this snippet then create a new post with some of the stop words above when you save it you will notice the permalink will get the stopwords removed. This would not cause problems with existing pages.

  • Vincent

    Awesome! It works perfectly in the new post while maintaining the structure of the old post. I’ve been waiting for this a long time, thank you very much.

    • Hi Vincent, no problem. Glad you enjoyed the snippet. 

      • Vincent

        Hi Kevin, I found a problem with this function, everything works fine but when I edit an old post this function automatically remove stop words from the slug. In my case this is a problem because this role was perfect for a classified site that I manage, users can edit their ads and every time this happens the structure of the URL changes. Anyway thank you very much for sharing this snippet. Regards.

      • Vincent

        Hi Kevin, I found a problem with this function, everything works fine but when I edit an old post this function automatically remove stop words from the slug. In my case this is a problem because this role was perfect for a classified site that I manage, users can edit their ads and every time this happens the structure of the URL changes. Anyway thank you very much for sharing this snippet. Regards.

        • I’m sure we can do something about that let me think about it a bit.

  • Pingback: Wordpress Better SEO automatically remove short words from URL – 400+ Wordpress code snippets for your blog()

  • Sandrine

    Great code !!! It even works fine with Qtranslate !
    Huge thanks

  • sanmi

    I wonder why you ask in the code if the user is admin… what happens if we don’t filter this? is there any security risk? I ask this because perhaps we have editors, authors… thank you for this useful snippet.

    • Nope no security risk, the idea was that people could filter based on capability or something along these lines.

  • Timothy Wood

    This may have been mentioned before but there are 2 spots that you could improve performance on this snippit. First move the the $keys_false var definition and $keys = explode(‘,’, $keys_false); outside of the $slug loop. You only should explode this once if you do need a comma separated list.Secondly, I would recommend getting rid of the comma separated list and using a prebuilt array to save on breaking apart the string and loading the array into mem already. If you do set it up like this:$stopwords = array(‘a’,’about’,’above’,’across’,’after’,’…’);foreach ($slug as $i => $word) {  if ( in_array($word, $stopwords) ) {     unset($slug[$k]);  }}return implode(‘-‘, $slug);

  • Download link is broken :(