SEO PHP Intermediate

Remove Stop Words From New Post Slugs

Shorten the URL WordPress creates from a new post's title by removing words like "the", "and" and "how".

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 20 lines
add_filter( 'wp_insert_post_data', function ( array $data, array $postarr ): array {
	if ( 'post' !== $data['post_type'] || in_array( $data['post_status'], [ 'draft', 'pending', 'auto-draft', 'inherit' ], true ) ) {
		return $data;
	}

	// Only touch slugs WordPress just generated: never change an existing or hand-typed one.
	$existing = ! empty( $postarr['ID'] ) ? (string) get_post_field( 'post_name', $postarr['ID'] ) : '';
	if ( '' !== $existing || $data['post_name'] !== sanitize_title( wp_unslash( $data['post_title'] ) ) ) {
		return $data;
	}

	$stop  = [ 'a', 'an', 'and', 'are', 'as', 'at', 'be', 'but', 'by', 'for', 'from', 'how', 'in', 'is', 'it', 'of', 'on', 'or', 'the', 'to', 'what', 'when', 'where', 'why', 'with', 'your' ];
	$words = array_values( array_diff( explode( '-', $data['post_name'] ), $stop ) );

	// Keep the original if removing stop words leaves too little.
	if ( count( $words ) >= 3 ) {
		$data['post_name'] = wp_unique_post_slug( implode( '-', $words ), (int) ( $postarr['ID'] ?? 0 ), $data['post_status'], $data['post_type'], (int) $data['post_parent'] );
	}
	return $data;
}, 10, 2 );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

A title like “How to Choose the Best Running Shoes for Your Feet” becomes a long slug full of filler words. This snippet trims them, giving /choose-best-running-shoes-feet/, when the post is first published.

How it works

WordPress creates the slug from the title when a post is first published, then passes everything through wp_insert_post_data before saving. The snippet only acts on blog posts being published for the first time whose slug matches the title exactly, meaning WordPress made it automatically.

It removes common English stop words. If at least three words remain, it uses the shorter slug, made unique with wp_unique_post_slug(). Otherwise it keeps the original.

Things to watch

  • Slugs of published posts are never changed, so existing URLs and links are safe.
  • If you type your own slug before publishing, it’s left alone.
  • The stop word list is English. Adjust it for other languages.

How to test it

Create a post called “How to Make the Perfect Cup of Coffee” and publish it. The slug should be make-perfect-cup-coffee.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only