WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/sanitize title

Description

Sanitizes title or use fallback title.

Specifically, HTML and PHP tags are stripped, and (in a 'save' context) accents are removed (accented characters are replaced with non-accented equivalents). Further filtering can be added via the plugin API by hooking the sanitize_title filter. If $title is empty and $fallback_title is set, the latter will be used.

Despite the name of this function, the returned value is intended to be suitable for use in a URL, not as a human-readable title.

Usage

<?php sanitize_title$title$fallback_title$context ?>

Parameters

$title
(string) (required) The string to be sanitized.
Default: None
$fallback_title
(string) (optional) A title to use if $title is empty.
Default: ''
$context
(string) (optional) The operation for which the string is sanitized.
Default: 'save'

Return Values

(string) 
The sanitized string.

Examples

WordPress Titles

To create the file name portion of a URL the same way that WordPress does use this:

<?php
  $new_url = sanitize_title('This Long Title is what My Post or Page might be');
  echo $new_url;
?>

It should return a formatted value, the output would be this: this-long-title-is-what-my-post-or-page-might-be

Notes

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.

Change Log

Source File

sanitize_title() is located in wp-includes/formatting.php.

Related

Functions

sanitize_title() is in a class of functions that help you sanitize potentially unsafe data which allow you to pass an arbitrary variable and receive the clean version based on data type. Others include:

Filters

See also index of Function Reference and index of Template Tags.