How To Shorten WordPress Slugs?

Slugs are a very important part of your website's URL structure. 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.

Learn more about WordPress slugs

If you need a better grasp of what a WordPress slug is and how it may be properly optimized, please visit our dedicated article.

Permalink example
The slug is typically located at the end of the permalink and is created by using the title of the page or post and converting it into a shorter, URL-friendly string.

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?

  1. 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.
  2. They are just more convenient. When a URL has fewer words, it becomes more convenient to type. This has yet another effect. The lengthy URLs take up a lot of space in the browser's address bar, making them difficult to read.

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.

  1. 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.
  2. 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.
Although you can still benefit from the information in this article without utilizing Permalink Manager, we recommend it for anyone seeking to change permalinks and explore additional customization options in WordPress.

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.

Click "Edit" to display the WordPress slug

You can now delete unnecessary words to make the slug short and descriptive. To update the slug, simply click on the "OK" button.

For slug shortening, you may utilize the built-in WordPress editor

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.

Disclaimer

Although this should function as intended for the vast majority of WordPress websites, the code supplied below may not be compatible with all configurations. It is usually a good idea to thoroughly test the code. If necessary, modify it to meet your individual requirements.

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 );

The code presented above is a WordPress filter function that changes the slug of a newly published post. This code is intended to limit the number of words in the post's slug whenever WordPress generates it.

Tip

By changing the value of $max_words within the function, you can set the maximum word count for the new slug.

How To Add The Code Snippet?

To use the code snippet provided above, you need to know where to put it. If you are unfamiliar with the term, code snippets allow you to customize and enhance the functionality of your website.

Adding PHP code snippets may seem complex if you are not familiar with the process, but it is actually quite simple. The task can be accomplished through a variety of methods. A way to manage code snippets is by using a plugin that is designed for that purpose. You can add, organize, and execute PHP code snippets directly from your WordPress dashboard by installing and activating a reputable plugin like "Code Snippets".

Alternatively, you can paste code snippets into your theme's functions.php file. This method requires an understanding of PHP as well as the structure of WordPress themes. To avoid any potential problems or conflicts, be careful when editing theme files.

Do you want to learn more about custom code snippets?

We recommend checking the dedicated post for more detailed instructions and alternate methods of adding code snippets to WordPress websites.

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. It is crucial to remember, however, that even if a code snippet is deleted, the slugs that were previously generated will not be altered and will stay unaffected.

Avatar for Maciej BisAbout Maciej Bis

Maciej has over 10 years of WordPress expertise and is the author of Permalink Manager, a popular WordPress plugin for managing permalinks. He has extensive experience in WordPress programming and has worked with clients from all around the world.

One response to “How To Shorten WordPress Slugs?”

Leave a Reply

Your email address will not be published. Required fields are marked *