When WordPress generates slugs for new posts, pages, or custom post type items, it automatically includes every word in the title. As a result, the slugs also incorporate "stop words" from the original title.
This can lead to long and unoptimized URLs, if the title contains words that add little value for search engines and visitors. Removing them can help create easier to read and more SEO-friendly URLs.
In this article, you will learn how to programmatically remove stop words from WordPress URLs using a simple code snippet, without any extra plugins.
What Are Stop Words in WordPress URLs?
Stop words refer to common words like "a", "the", "in", or "of". When WordPress generates native slugs that are the core part of the canonical URLs, it includes these words by default, leading to unnecessarily lengthy permalinks.
For example, a post titled "The Complete Guide to Building a WordPress Blog" might generate this URL:
https://example.com/the-complete-guide-to-building-a-wordpress-blog
The red words are meaningless for search engine algorithms, so we can shorten them to make them readable.
https://example.com/complete-guide-building-wordpress-blog
Why Remove Stop-Words?
Shortening WordPress URLs by removing stop words is a good idea for several reasons. By eliminating them from your WordPress permalinks, you can:
- Improve readability:
It is simpler for users to understand and remember URLs that are more concise. Additionally, they are less likely to get messed up when shared and simply look better when shared via social media. - Enhance SEO:
While keywords in URLs are no longer as important anymore, having clear URLs can result in more clicks from search engine users.
Should Stop Words Always Be Removed?
In some cases, keeping stop words and URLs intact is preferable for user experience. They are often necessary to keep the meaning and context of your URLs.
Here are some examples where removing them can change the meaning entirely:
https://example.com/where-to-eat-in-paris ==> https://example.com/where-eat-paris
https://example.com/the-art-of-war ==> https://example.com/art-war
https://example.com/the-sound-of-silence ==> https://example.com/sound-silence
Yoast SEO, a popular WordPress plugin, previously had an option to automatically remove stop words from slugs, but it was removed in March 2018.
Each website necessitates an individual approach, so the final decision may differ depending on your needs and SEO strategy.
The Solution
To remove stop-words from slugs in WordPress automatically, simply add the following code to your website's codebase. If necessary, feel free to extend and change it.
For instance, you can change the $stop_words array to add additional stop words depending on your needs.
Custom Code Snippet
function pm_is_stop_word($word) {
	$stop_words = array(
		'a', 'an', 'and', 'are', 'as', 'at',
		'be', 'by', 'for', 'from', 'has',
		'in', 'is', 'it', 'of', 'on', 'that',
		'the', 'this', 'to', 'was', 'were',
		'will', 'with'
	);
	return in_array($word, $stop_words);
}
function pm_remove_stop_words_from_slug($slug) {
	// Split slug into words
	$words = explode('-', $slug);
	$filtered_words = array();
	foreach ($words as $word) {
		if (pm_is_stop_word($word)) {
			continue;
		}
		$filtered_words[] = $word;
	}
	// Handle empty slugs
	if (empty($filtered_words)) {
		return $slug;
	}
	// Rebuild and return the clean slug
	return implode('-', $filtered_words);
}
add_filter('wp_unique_post_slug', 'pm_remove_stop_words_from_slug');
This snippet is intended to run whenever a unique slug is generated by WordPress for new post, page or category. In terms of code complexity, it is really simple. It uses the wp_unique_post_slug filter, to remove the pre-defined words from all newly generated slugs.
How To Handle Existing URLs?
If you want to remove stop-words from existing posts' and terms' URL, you will need to regenerate them manually.
In order to do so, you can use the Permalink Manager. This plugin includes a "Regenerate/reset" tool, which, when set to "Native slugs" mode, allows you to quickly recreate native slugs.
When removing stop words from existing URLs, make sure to set up fallback redirects from the old URLs to the new ones. The redirects will prevent "404 Not Found" errors and help you keep your SEO rankings by passing link equity.
Leave a Reply