Automatically Remove Short Words From WP Permalinks

Updated 02/02/2024, Posted 02/02/2024 by James Parsons James Parsons 0 Comments

WordPress automatically saves a permalink for you when creating a new post. Most people edit this before publishing it to remove the low-value keywords from it, otherwise it would be a giant URL. For example:

  • Guide: How to Repair a Roof on a Budget in 2024

in WordPress, the title they would choose by default is:

  • /guide-how-to-repair-a-roof-on-a-budget-in-2024

With this functions.php code, it will remove any words that are 4 characters or less. It would become:

  • /guide-repair-roof-budget

Pretty handy, right? Here's the code; simply plop it at the bottom of your functions.php file in your theme:

/*
 * Removes short low-value words from the automatically-generated permalink
 */ 
 
function modify_draft_permalinks($data) {
  if ($data['post_status'] != 'draft') {
      return $data; 
  }
  $title = $data['post_title'];
  $small_words = '/\b(\w{1,3})\b/'; // Match words of 1 to 3 characters.
  $new_title = preg_replace($small_words, '', $title); 
  $new_title = sanitize_title_with_dashes($new_title);
  $data['post_name'] = $new_title;
  return $data; 
}
add_filter('wp_insert_post_data', 'modify_draft_permalinks', 10, 1);

Addressing potential questions:

Will this mess with my exisiting permalinks?

No, this is only functions when creating new posts that are still in the "Draft" state. They are for posts that are not published yet.

WordPress will autosave your work after 30 seconds or so, and they choose a permalink for you. This replaces that functionality and chooses a shorter, better permalink.

Can I still set my own permalink?

Yes, this just affects the automatic recommendation. You can still replace it manually and this code won't overwrite your changes.

Can I change the length?

Yes. By default, this removes any word that is 3 characters long or smaller.

If you want to make it only remove 2 characters words or less, for instance, then change the $small_words line to:

$small_words = '/\b(\w{1,2})\b/';

What's the point?

Many of our clients write their own content in addition to our work that we do for their sites. Unfortunately they don't always optimize their permalinks, so you end up with gigantic URLs.

This is a nice set-it-and-forget-it thing that improves the default WordPress functionality by trimming low-value words from URLs.

Related Code Snippets

Written by James Parsons

Hi, I'm James Parsons! I founded Content Powered, a content marketing agency where I partner with businesses to help them grow through strategic content. With nearly twenty years of SEO and content marketing experience, I've had the joy of helping companies connect with their audiences in meaningful ways. I started my journey by building and growing several successful eCommerce companies solely through content marketing, and I love to share what I've learned along the way. You'll find my thoughts and insights in publications like Search Engine Watch, Search Engine Journal, Forbes, Entrepreneur, and Inc, among others. I've been fortunate to work with wonderful clients ranging from growing businesses to Fortune 500 companies like eBay and Expedia, and helping them shape their content strategies. My focus is on creating optimized content that resonates and converts. I'd love to connect – the best way to contact me is by scheduling a call or by email.