Generate slugs in WordPress: sanitize_title, accents, and bulk URL planning
WordPress quietly slugifies every post title you save and that slug becomes part of the permalink. Accented titles produce surprising results, and once a post is published the slug is the URL the world knows. Planning slugs before you click publish is the cheap fix.
sanitize_title is the function under the hood
Every time you save a post, WordPress runs the title through sanitize_title() to derive the default slug. That function calls remove_accents(), lowercases everything, replaces whitespace with dashes, and strips characters that are not alphanumerics, dashes or underscores.
$slug = sanitize_title( 'Cรณmo posicionar tu web en 2026: guรญa SEO' );
// "como-posicionar-tu-web-en-2026-guia-seo"
If you are scripting bulk imports or migrations, this is the function to call. Do not roll your own slugifier inside a plugin: sanitize_title is the canonical behavior every theme, plugin and SEO tool downstream expects.
The accent quirk depends on locale
remove_accents() strips diacritics for most Latin scripts. For non-Latin scripts the behavior depends on the active locale. A site in zh_CN keeps the original Chinese characters in the slug. A site in en_US running on a hosting provider that re-encodes the URL may percent-encode them, turning your clean slug into %E5%8C%97%E4%BA%AC.
If you serve an international audience, the safest move is to set the slug yourself in the post editor (the URL field under the title) using ASCII characters that you have control over, rather than trusting the auto-generated slug for non-Latin titles. Plugins like Yoast SEO surface this field prominently for a reason.
Slugs lock in. Plan before publish.
Once a post is published, the slug is in your sitemap, in Google's index, and possibly in inbound links. Changing it later means setting up a 301 redirect or watching SEO equity bleed. WordPress can do that redirect via wp_old_slug_redirect, but only if the old slug was ever attached to the post; if you change the slug before publishing you do not get that fallback.
The pragmatic workflow:
1. Draft the post. 2. Generate the slug from the working title using a deterministic tool. 3. Paste it into the URL field manually before clicking publish. 4. Never touch it again.
Doing step 2 outside the WordPress editor lets you batch-plan slugs across an editorial calendar in a spreadsheet, which is much faster than opening 30 drafts.
WP-CLI for bulk fixes
If you inherited a site with messy slugs, WP-CLI is the tool for surgical batch updates:
wp post list --post_type=post --field=ID --post_status=publish |
while read ID; do
TITLE=$(wp post get $ID --field=post_title)
NEW=$(wp eval "echo sanitize_title('$TITLE');")
wp post update $ID --post_name="$NEW"
done
Always back up first, run on a staging copy first, and queue redirects from the old slugs to the new ones with a plugin like Redirection. SEO will recover; broken inbound links will not.
Working example
php<?php
// Functions.php helper to expose a clean slug suggestion in the editor
add_filter( 'name_save_pre', 'aldeacode_slugify_on_save' );
function aldeacode_slugify_on_save( $slug ) {
if ( empty( $slug ) ) {
$title = isset( $_POST['post_title'] ) ? wp_unslash( $_POST['post_title'] ) : '';
$slug = sanitize_title( $title );
}
return $slug;
}
// Direct use anywhere in WP
$slug = sanitize_title( 'Neo-Brutalist Web Design: A Manifesto' );
// "neo-brutalist-web-design-a-manifesto" Just need the result?
When you are planning a content calendar in a spreadsheet and want clean slugs before drafting in WordPress, the browser-based slug generator does the same NFD-then-strip dance as sanitize_title, but in batch. Paste a list of titles, copy back a list of slugs, paste each into the URL field when you publish.
Open URL Slug Generator โFrequently asked questions
Why does my slug have garbled accented characters?
remove_accents handles common Latin diacritics but the locale matters. Set the slug manually in the URL field under the title for accented or non-Latin titles, especially before the post is published.
Does WordPress redirect old slugs automatically?
Yes if the post was published with the old slug at any point. wp_old_slug_redirect intercepts requests and 301s to the new URL. If you change the slug before first publish there is no historical record and no redirect.
Can two posts share the same slug?
No within the same post type. WordPress appends -2, -3 and so on to make the slug unique. If you really want a clean slug, change the existing post's slug first or trash it.