Slugs play a crucial role in the structure of your website's URL addresses. They follow the domain name and define specific pages or posts. This article will show how you can adjust and make them shorter.
By default, WordPress generates slugs based on the initial title of the article. Unfortunately, the generated slugs are usually too lengthy and therefore hard to read.
First, let us talk about what slugs are and why they are crucial for SEO before we get into how to shorten them.
Understanding the Importance of Short Slugs for SEO
Before we go into how to shorten WordPress slugs, you need know what slugs are. A slug, to put it simply, is a component of a URL that indicates a particular page or post. WordPress generates slugs based on the content's first title.
As an example, consider the blog page "The Ultimate Guide to WordPress SEO". The default slug in this situation would be "the-ultimate-guide-to-wordpress-seo". This slug helps both search engines and human users understand the page's content. Sometimes, though, WordPress can generate a slug that is just too lengthy to be readable. Truncating slugs may be really useful in this situation.
If you need a better grasp of what a WordPress slug is and how it may be properly optimized, please visit our dedicated article.
What Is the Maximum Length Allowed For a Slug?
There is a maximum length for slugs, although it is not a problem for the average user. If the slug exceeds 200 characters, WordPress will shorten it automatically. To be more exact, this occurs in the following line inside sanitize_title_with_dashes() function:
The function turns the title into a slug by removing any punctuation and substituting dashes into spaces. It then limits the slug to a maximum of 200 characters.
This keeps slugs short and uniform while yet allowing each post to be identified uniquely.
Why Should You Shorten WordPress Slugs?
Shortening slugs can have a positive impact on SEO and enhance the overall user experience. Why should you consider it?
- The shorter URLs are easier for both search engines and human visitors to understand. They clearly describe the content and make people more likely to click on them in search results.
- They are just more convenient. When a URL has fewer words, it becomes more convenient to type. This has yet another effect. The long URLs are hard to read since they take up a lot of space in the browser's address bar.
Shortening WordPress slugs
Now that you are aware of what a slug is and why they are crucial for SEO, let us look at some effective ideas for optimizing your slugs. The two optimization tips outlined below should help you create slugs that are short, keyword-rich, and search engine-friendly. This should eventually increase your website's visibility and generate more organic traffic.
- Remove all stop words
Stop words such as "in," "of," "to," and "for" should not be used in your slugs since they add extra length and do not significantly increase search relevancy. - Pay attention to keywords
Include keywords in your slugs to increase their relevancy and exposure in search engines. However, avoid keyword stuffing, since it may result in search engine penalties.
Method 1. Personalizing Slugs in the WordPress Editor
To edit the slug in the WordPress editor, simply follow these steps. To begin, access the WordPress editor for the specific page or post you want to work on. The permalink field is located underneath the heading. Click the "Edit" button next to the permalink to make changes.
You can now delete unnecessary words to make the slug short and descriptive. To update the slug, simply click on the "OK" button.
Method 2. Shorten Slugs Automatically
To make things easier, you can set up a custom function to modify the way slugs are generated. This would save you the hassle of manually modifying each one. It does involve a bit of PHP knowledge, but it should be straightforward to set up for most users.
To implement this, we will need to modify the post's unique slug just before it is saved to the database, using the
wp_unique_post_slug
WordPress filter hook. To clarify, WordPress uses this hook when a post is created or changed.
Although this should function as intended for the vast majority of WordPress websites, the code supplied below may not be compatible with all configurations.
function pm_slug_limit_words( $slug, $post_id, $post_status, $post_type, $post_parent, $original_slug ) {
	// The new slug's max. word count can be changed below
	$max_words = 3;
	if ( is_numeric( $post_id ) ) {
		$post = get_post( $post_id );
		// Stop if the slug for this post was already generated
		if ( ! empty( $post->post_name ) ) {
			return $slug;
		}
		if ( ! empty( $slug ) && substr_count( $slug, '-' ) + 1 > $max_words ) {
			$words = explode( '-', $slug );
			$slug = implode( '-', array_slice( $words, 0, $max_words ) );
		}
	}
	return $slug;
}
add_filter( 'wp_unique_post_slug', 'pm_slug_limit_words', 100, 6 );
By changing the value of
$max_words
within the function, you can set the maximum word count for the new slug.
The code presented above is a WordPress filter function that changes the slug of a newly published post. The purpose of this code is to limit the length of the post slug that WordPress automatically creates.
FAQ
Can I Shorten Slugs In WordPress Without Adding More Plugins?
No, you do not have to use a plugin like Permalink Manager to shorten slugs in WordPress. You can manually alter and personalize slugs using the platform's built-in functionality. All the instructions are located above in this article.
You may easily shorten and improve the slug field in the WordPress editor while creating or modifying a page or post. This keeps your website simple and lessens plugin dependencies while allowing slug customization without the use of additional plugins.
Can I Change The Slug Of Existing Posts Using This Method?
The code snippet in this article is intended to change the slug of newly published content in WordPress. It is not designed to change the slugs of existing posts or pages. If a post already has a generated slug, the method will return it unchanged in order to avoid overwriting it.
Can I Use This Function For Custom Post Types?
The provided code snippet may be used with other custom post types, such as WooCommerce products, as well. The code snippet works with any WordPress post type, including custom post types like WooCommerce products.
The given code allows you to limit the number of words in the slug of a newly published post, and it applies to all post types, including inbuilt and custom ones.
How Do I Revert Back To The Default Slug Generation In WordPress?
Yes, you may return to the default slug generation in WordPress by deleting the code snippet from your website's source. Please keep in mind that removing this code snippet will not change the slugs that were already created.
Love this plugin.
Can I automatically remove hyphen's from my WordPress slugs?