Sanitizes a string into a slug, which can be used in URLs or HTML attributes.
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.
$titlestringrequired
$fallback_titlestringoptional
Default:''
$contextstringoptional
'save', the string runs through remove_accents() .'save'.Default:'save'
The ‘save’ context is used most often when saving a value in the database, but is used for other purposes as well. The ‘query’ context is used by sanitize_title_for_query() when the value is going to be used in the WHERE clause of a query.
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;
}
Filters a sanitized title string.
| Version | Description |
|---|---|
| 1.0.0 | Introduced. |
© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/sanitize_title