How To Shorten WordPress Slugs?

The primary objective of this article is to provide you with tips for enhancing and shortening slugs. In this piece, we will explain why this is so important and then show you a quick and simple way for making URLs that are both functional and easy to understand.

When it comes to WordPress, a slug is an extremely important element in determining the URL of your website. It is the portion of the URL that appears after the domain name and defines a particular page or post. WordPress creates slugs for you automatically depending on the title of your content, but they frequently end up being long, wordy, and not search engine friendly.

Understanding the Importance of Short Slugs for SEO

Before getting into the topic of shortening WordPress slugs, it is important to understand what slugs are and their importance in the WordPress ecosystem. A slug is a section of a URL that represents a specific page or post on a website. WordPress generates slugs depending on the title of the content, but users can edit and optimize them to their liking.

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. However, there are times when the default slug is overly long and confusing, which is why the practice of truncating slugs becomes highly beneficial.

Do you want to 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?

Slug shortening in WordPress can improve both search engine optimization (SEO) and the user experience in a number of ways. Here are some key reasons why you should consider doing it:

  1. Shorter slugs are easier to read and understand for both search engines and human visitors, improving readability. They make it more compelling for users to click on the link by giving a clear and concise description of the page or post's topic.
  2. When determining the relevance and ranking of a page, search engines take into account the words in the URL. You may improve the SEO potential of your content by removing stop words from your slugs and your keywords.

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. This will give you the option to change the slug manually.

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

A more convenient and easy option is to use a custom function in WordPress that alters the format of newly generated slugs. This method is especially useful because it eliminates the need to manually modify each individual slug. It requires a basic understanding of PHP, but most users should find it easy to implement.

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

The following code is provided "as is" and may not function in every WordPress website configuration due to third-party plugins and your own custom code. All in all, It is always a good idea to test and modify the code to fit your needs.

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. If the post already has a generated slug, the function will return that slug unaltered to ensure that it is not overwritten.

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 for your WordPress website, 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 may manually add code snippets by changing your theme's functions.php file. To use this method, you need to have a basic understanding of PHP and 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.

Leave a Reply

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