sanitize_title( string $title, string $fallback_title = '', string $context = 'save' )

Sanitizes a title, or returns a fallback title.


Description Description

Specifically, HTML and PHP tags are stripped. Further actions can be added via the plugin API. If $title is empty and $fallback_title is set, the latter will be used.


Parameters 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

Default value: 'save'


Top ↑

Return Return

(string) The sanitized string.


Top ↑

Source 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;
}

Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    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

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