sanitize_title() WordPress Function
The sanitize_title() function is a built-in WordPress function that is used to clean up a title before it is used in the URL. This function is useful for making sure that a title is safe to use in a URL, and it also makes sure that the title is correctly formatted for use in a URL.
sanitize_title( string $title, string $fallback_title = '', string $context = 'save' ) #
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.
Parameters
- $title
(string)(Required)The string to be sanitized.
- $fallback_title
(string)(Optional) A title to use if $title is empty.
Default value: ''
- $context
(string)(Optional) The operation for which the string is sanitized. When set to 'save', the string runs through remove_accents().
Default value: 'save'
Return
(string) The sanitized string.
More Information
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.
Source
File: wp-includes/formatting.php
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;
}
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
| Version | Description |
|---|---|
| 1.0.0 | Introduced. |