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.


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.


The string to be sanitized.
A title to use if $title is empty.


The operation for which the string is sanitized.
When set to 'save', the string runs through remove_accents() .
Default 'save'.



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.


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;


apply_filters( ‘sanitize_title’, string $title, string $raw_title, string $context )

Filters a sanitized title string.



User Contributed Notes

  1. Skip to note 4 content

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

      $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:


  2. Skip to note 5 content

    As was noted in the codex reference page for this function:

    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.

    So My Excellent Sentence becomes my-excellent-sentence for example.

    This is because this function applies a filter of the same name to which WordPress adds sanitize_title_with_dashes by default.

  3. Skip to note 6 content

    If title is Chinese, its not suitable as a HTML attributes.
    Because the function will converts words to ASCII characters.It will be a disaster.

    $str = "這是字串";
    $str = sanitize_title( $str );
    echo $str; // it will echo "%e9%80%99%e6%98%af%e5%ad%97%e4%b8%b2"

You must log in before being able to contribute a note or feedback.