wppaste
WordPress

sanitize_title( string $title, string $fallback_title = '', string $context = 'save' ): string

Since
1.0.0
Source
wp-includes/formatting.php:2228

Runs a raw title through the core slug-sanitization filter to produce a lowercase, hyphen-safe string suitable for permalinks or HTML id attributes. The $context argument controls whether accented characters are stripped first (only when set to 'save', the default), and $fallback_title supplies a replacement when the sanitized result comes back empty. Pair it with sanitize_title_with_dashes() when you want the default filter behavior without going through the 'sanitize_title' hook.

Sanitizes a string into a slug, which can be used in URLs or HTML attributes.

Description

By default, converts accent characters to ASCII characters and further limits the output to alphanumeric characters, underscore (_) and dash (-) through the 'sanitize_title' filter.

If $title is empty and $fallback_title is set, the latter will be used.

Compatibility

WordPress
since 1.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$titlestring
The string to be sanitized.
$fallback_titlestringoptional
A title to use if $title is empty. Default empty.Default: ''
$contextstringoptional
The operation for which the string is sanitized.
When set to 'save', the string runs through remove_accents().
Default 'save'.Default: 'save'

Return value

string
The sanitized string.

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Convert a post title into a URL-safe slug

Build a slug from the title of post 1 and from a title containing accented characters to see remove_accents() at work.

$post_title = get_the_title( 1 );
$accented_title = 'Café con leche';

echo 'From "' . esc_html( $post_title ) . '": ' . esc_html( sanitize_title( $post_title ) ) . '<br>';
echo 'From "' . esc_html( $accented_title ) . '": ' . esc_html( sanitize_title( $accented_title ) );

Fall back to a generated slug when the title is empty

Simulate an import routine that hands sanitize_title() an empty title and a fallback based on the post ID, then compare context 'save' with context 'query'.

$empty_title = '';
$post_id = 4;

$fallback_slug = sanitize_title( $empty_title, 'post-' . $post_id );
echo 'Fallback slug: ' . esc_html( $fallback_slug ) . '<br>';

$raw_title = 'Ñandú Ranch';
$save_slug = sanitize_title( $raw_title, '', 'save' );
$query_slug = sanitize_title( $raw_title, '', 'query' );

echo "Context 'save': " . esc_html( $save_slug ) . '<br>';
echo "Context 'query': " . esc_html( $query_slug );

Only the default 'save' context runs remove_accents() first, so other contexts can leave non-ASCII characters to be handled purely by the 'sanitize_title' filter chain.

Common problems and fixes · 3

Why does my fallback title show up with weird characters or capital letters instead of being cleaned up?

The source only assigns $fallback_title to $title when the sanitized result is empty or false; it never runs the fallback back through remove_accents() or the 'sanitize_title' filter. Whatever string you pass as $fallback_title is used exactly as given.

Why do accented letters survive sanitize_title() sometimes?

remove_accents() only runs when $context equals 'save', which is the default but not the only value core code passes. Calls with 'query', 'display', or other contexts skip that step entirely, leaving accent handling to whatever is hooked on the 'sanitize_title' filter.

Why is my slug different from what sanitize_title_with_dashes() produces on its own?

sanitize_title() does not build the slug itself past accent removal; it hands the string to the 'sanitize_title' filter (see hooks), whose default callback is sanitize_title_with_dashes(). Any plugin or theme hooked onto that filter can change the result, so calling sanitize_title() and sanitize_title_with_dashes() directly can diverge.

Alternatives and related functions

sanitize_title_with_dashes
When you want the default hyphenated slug logic without running it through the filterable 'sanitize_title' hook.
sanitize_key
When sanitizing an internal identifier like an option name or meta key rather than a public-facing slug.
remove_accents
When you only need accented characters converted to ASCII and don't want the rest of the slug filtering.
wp_unique_post_slug
When you need a slug guaranteed not to collide with an existing post, page, or attachment.

Performance profile

How much work a call to sanitize_title() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
17–23

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 23.

Plugin surface
1 hook

Third-party callbacks on 'sanitize_title' run inside this call, and their cost is not bounded by anything here.

Called by
50

50 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost sanitize_title() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$context !== "save"17–19apply_filters()
$context === "save"21–23remove_accents(), apply_filters()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 23 instructions, 17–23 executed per call, 3 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while sanitize_title() runs, in this order:

  1. apply_filters( sanitize_title )filterline 2244 (+16 into the body)

    Filters a sanitized title string.

Uses · 2

Used by · 50

Show all 50

Source code

function sanitize_title( $title, $fallback_title = '', $context = 'save' ) {	$raw_title = $title; 	if ( 'save' === $context ) {		$title = remove_accents( $title );	} 	/**	 * Filters a sanitized title string.	 *	 * @since 1.2.0	 *	 * @param string $title     Sanitized title.	 * @param string $raw_title The title prior to sanitization.	 * @param string $context   The context for which the title is being sanitized.	 */	$title = apply_filters( 'sanitize_title', $title, $raw_title, $context ); 	if ( '' === $title || false === $title ) {		$title = $fallback_title;	} 	return $title;}

Changelog

Introduced in 1.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/formatting.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.