On this page
sanitize_title( string $title, string $fallback_title = '', string $context = 'save' ): string
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
$titlestring Required-
The string to be sanitized.
$fallback_titlestring Optional-
A title to use if $title is empty.
Default:
'' $contextstring Optional-
The operation for which the string is sanitized.
When set to'save', the string runs through remove_accents() .
Default'save'.Default:
'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. View all references
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;
}
Hooks
- apply_filters( 'sanitize_title',
string $title ,string $raw_title ,string $context ) -
Filters a sanitized title string.
Related
Uses
| Uses | Description |
|---|---|
| remove_accents() wp-includes/formatting.php | Converts all accent characters to ASCII characters. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
Used By
| Used By | Description |
|---|---|
| WP_Theme_JSON::set_spacing_sizes() wp-includes/class-wp-theme-json.php | Sets the spacingSizes array based on the spacingScale values from theme.json. |
| WP_Theme_JSON::get_layout_styles() wp-includes/class-wp-theme-json.php | Gets the CSS layout rules for a particular block from theme.json layout definitions. |
| _register_remote_theme_patterns() wp-includes/block-patterns.php | Registers patterns from Pattern Directory provided by a theme’s |
| _load_remote_featured_patterns() wp-includes/block-patterns.php | Register |
| _load_remote_block_patterns() wp-includes/block-patterns.php | Register Core’s official patterns from wordpress.org/patterns. |
| WP_REST_Attachments_Controller::insert_attachment() wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php | Inserts the attachment post in the database. Does not update the attachment meta. |
| WP_Customize_Manager::import_theme_starter_content() wp-includes/class-wp-customize-manager.php | Imports theme starter content into the customized state. |
| WP_Customize_Manager::prepare_starter_content_attachments() wp-includes/class-wp-customize-manager.php | Prepares starter content attachments. |
| wp_update_custom_css_post() wp-includes/theme.php | Updates the |
| wp_get_custom_css_post() wp-includes/theme.php | Fetches the |
| WP_REST_Controller::sanitize_slug() wp-includes/rest-api/endpoints/class-wp-rest-controller.php | Sanitizes the slug value. |
| WP_Customize_Nav_Menus::insert_auto_draft_post() wp-includes/class-wp-customize-nav-menus.php | Adds a new |
| WP_Customize_Nav_Menu_Setting::filter_wp_get_nav_menus() wp-includes/customize/class-wp-customize-nav-menu-setting.php | Filters the wp_get_nav_menus() result to ensure the inserted menu object is included, and the deleted one is removed. |
| WP_Customize_Nav_Menu_Setting::filter_wp_get_nav_menu_object() wp-includes/customize/class-wp-customize-nav-menu-setting.php | Filters the wp_get_nav_menu_object() result to supply the previewed menu object. |
| wp_install_maybe_enable_pretty_permalinks() wp-admin/includes/upgrade.php | Maybe enable pretty permalinks on installation. |
| WP_Plugins_List_Table::single_row() wp-admin/includes/class-wp-plugins-list-table.php | |
| update_nag() wp-admin/includes/update.php | Returns core update notification message. |
| make_site_theme() wp-admin/includes/upgrade.php | Creates a site theme. |
| wp_install_defaults() wp-admin/includes/upgrade.php | Creates the initial content for a newly-installed site. |
| add_menu_page() wp-admin/includes/plugin.php | Adds a top-level menu page. |
| media_handle_upload() wp-admin/includes/media.php | Saves a file submitted from a POST request and create an attachment post for it. |
| media_handle_sideload() wp-admin/includes/media.php | Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload() . |
| get_sample_permalink() wp-admin/includes/post.php | Returns a sample permalink based on the post name. |
| _wp_ajax_add_hierarchical_term() wp-admin/includes/ajax-actions.php | Ajax handler for adding a hierarchical term. |
| wp_ajax_add_link_category() wp-admin/includes/ajax-actions.php | Ajax handler for adding a link category. |
| list_core_update() wp-admin/update-core.php | Lists available core updates. |
| sanitize_title_for_query() wp-includes/formatting.php | Sanitizes a title with the ‘query’ context. |
| register_widget_control() wp-includes/deprecated.php | Registers widget control callback for customizing options. |
| register_sidebar_widget() wp-includes/deprecated.php | Register widget for sidebar with backward compatibility. |
| get_category_by_path() wp-includes/category.php | Retrieves a category based on URL containing the category slug. |
| wp_update_term() wp-includes/taxonomy.php | Updates term based on arguments provided. |
| wp_insert_term() wp-includes/taxonomy.php | Adds a new term to the database. |
| term_exists() wp-includes/taxonomy.php | Determines whether a taxonomy term exists. |
| register_taxonomy() wp-includes/taxonomy.php | Creates or modifies a taxonomy object. |
| permalink_anchor() wp-includes/link-template.php | Displays the permalink anchor for the current post. |
| WP_Admin_Bar::add_node() wp-includes/class-wp-admin-bar.php | Adds a node to the menu. |
| wp_insert_user() wp-includes/user.php | Inserts a user into the database. |
| wp_insert_post() wp-includes/post.php | Inserts or update a post. |
| dynamic_sidebar() wp-includes/widgets.php | Display dynamic sidebar. |
| is_active_sidebar() wp-includes/widgets.php | Determines whether a sidebar contains widgets. |
Changelog
| Version | Description |
|---|---|
| 1.0.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/sanitize_title