sanitize_title

Sanitizes a title, or returns a fallback title.

Description

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

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.

Returns (string)

The sanitized string.

Parameters (3)

0. $title (string)
The string to be sanitized.
1. $fallback_title — Optional. (string) => ''
A title to use if $title is empty.
2. $context — Optional. (string) => 'save'
The operation for which the string is sanitized

Usage

  1. if ( !function_exists( 'sanitize_title' ) ) { 
  2. require_once ABSPATH . WPINC . '/formatting.php'; 
  3.  
  4. // The string to be sanitized. 
  5. $title = ''; 
  6.  
  7. // Optional. A title to use if $title is empty. 
  8. $fallback_title = ''; 
  9.  
  10. // Optional. The operation for which the string is sanitized 
  11. $context = 'save'; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = sanitize_title($title, $fallback_title, $context); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/formatting.php  
  1. function sanitize_title( $title, $fallback_title = '', $context = 'save' ) { 
  2. $raw_title = $title; 
  3.  
  4. if ( 'save' == $context ) 
  5. $title = remove_accents($title); 
  6.  
  7. /** 
  8. * Filters a sanitized title string. 
  9. * @since 1.2.0 
  10. * @param string $title Sanitized title. 
  11. * @param string $raw_title The title prior to sanitization. 
  12. * @param string $context The context for which the title is being sanitized. 
  13. */ 
  14. $title = apply_filters( 'sanitize_title', $title, $raw_title, $context ); 
  15.  
  16. if ( '' === $title || false === $title ) 
  17. $title = $fallback_title; 
  18.  
  19. return $title;