sanitize_titlefilter-hookWP 1.2.0

Allows changing a string passed to sanitize_title() after unwanted characters have been removed.

Usage

add_filter( 'sanitize_title', 'wp_kama_sanitize_title_filter', 10, 3 );

/**
 * Function for `sanitize_title` filter-hook.
 * 
 * @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.
 *
 * @return string
 */
function wp_kama_sanitize_title_filter( $title, $raw_title, $context ){

	// filter...
	return $title;
}
$title(string)
The sanitized string.
$raw_title(string)
The original, unsanitized string.
$context(string)
The context in which the string is being sanitized.

Examples

#1 Transliterate a string

The code is taken from the Cyr to Lat enhanced plugin. Unicode escape sequences are used for the source characters so the example contains no locale-specific literal text while preserving the same PHP behavior.

add_filter('sanitize_title', 'ctl_sanitize_title', 9);

function ctl_sanitize_title($title) {
	global $wpdb;

	$iso9_table = array(
		"\u{0410}" => 'A', "\u{0411}" => 'B', "\u{0412}" => 'V', "\u{0413}" => 'G', "\u{0403}" => 'G',
		"\u{0490}" => 'G', "\u{0414}" => 'D', "\u{0415}" => 'E', "\u{0401}" => 'YO', "\u{0404}" => 'YE',
		"\u{0416}" => 'ZH', "\u{0417}" => 'Z', "\u{0405}" => 'Z', "\u{0418}" => 'I', "\u{0419}" => 'J',
		"\u{0408}" => 'J', "\u{0406}" => 'I', "\u{0407}" => 'YI', "\u{041A}" => 'K', "\u{040C}" => 'K',
		"\u{041B}" => 'L', "\u{0409}" => 'L', "\u{041C}" => 'M', "\u{041D}" => 'N', "\u{040A}" => 'N',
		"\u{041E}" => 'O', "\u{041F}" => 'P', "\u{0420}" => 'R', "\u{0421}" => 'S', "\u{0422}" => 'T',
		"\u{0423}" => 'U', "\u{040E}" => 'U', "\u{0424}" => 'F', "\u{0425}" => 'H', "\u{0426}" => 'TS',
		"\u{0427}" => 'CH', "\u{040F}" => 'DH', "\u{0428}" => 'SH', "\u{0429}" => 'SHH', "\u{042A}" => '',
		"\u{042B}" => 'Y', "\u{042C}" => '', "\u{042D}" => 'E', "\u{042E}" => 'YU', "\u{042F}" => 'YA',
		"\u{0430}" => 'a', "\u{0431}" => 'b', "\u{0432}" => 'v', "\u{0433}" => 'g', "\u{0453}" => 'g',
		"\u{0491}" => 'g', "\u{0434}" => 'd', "\u{0435}" => 'e', "\u{0451}" => 'yo', "\u{0454}" => 'ye',
		"\u{0436}" => 'zh', "\u{0437}" => 'z', "\u{0455}" => 'z', "\u{0438}" => 'i', "\u{0439}" => 'j',
		"\u{0458}" => 'j', "\u{0456}" => 'i', "\u{0457}" => 'yi', "\u{043A}" => 'k', "\u{045C}" => 'k',
		"\u{043B}" => 'l', "\u{0459}" => 'l', "\u{043C}" => 'm', "\u{043D}" => 'n', "\u{045A}" => 'n',
		"\u{043E}" => 'o', "\u{043F}" => 'p', "\u{0440}" => 'r', "\u{0441}" => 's', "\u{0442}" => 't',
		"\u{0443}" => 'u', "\u{045E}" => 'u', "\u{0444}" => 'f', "\u{0445}" => 'h', "\u{0446}" => 'ts',
		"\u{0447}" => 'ch', "\u{045F}" => 'dh', "\u{0448}" => 'sh', "\u{0449}" => 'shh', "\u{044A}" => '',
		"\u{044B}" => 'y', "\u{044C}" => '', "\u{044D}" => 'e', "\u{044E}" => 'yu', "\u{044F}" => 'ya'
	);
	$geo2lat = array(
		"\u{10D0}" => 'a', "\u{10D1}" => 'b', "\u{10D2}" => 'g', "\u{10D3}" => 'd', "\u{10D4}" => 'e', "\u{10D5}" => 'v',
		"\u{10D6}" => 'z', "\u{10D7}" => 'th', "\u{10D8}" => 'i', "\u{10D9}" => 'k', "\u{10DA}" => 'l', "\u{10DB}" => 'm',
		"\u{10DC}" => 'n', "\u{10DD}" => 'o', "\u{10DE}" => 'p',"\u{10DF}" => 'zh',"\u{10E0}" => 'r',"\u{10E1}" => 's',
		"\u{10E2}" => 't',"\u{10E3}" => 'u',"\u{10E4}" => 'ph',"\u{10E5}" => 'q',"\u{10E6}" => 'gh',"\u{10E7}" => 'qh',
		"\u{10E8}" => 'sh',"\u{10E9}" => 'ch',"\u{10EA}" => 'ts',"\u{10EB}" => 'dz',"\u{10EC}" => 'ts',"\u{10ED}" => 'tch',
		"\u{10EE}" => 'kh',"\u{10EF}" => 'j',"\u{10F0}" => 'h'
	);
	$iso9_table = array_merge($iso9_table, $geo2lat);

	$locale = get_locale();
	switch ( $locale ) {
		case 'bg_BG':
			$iso9_table["\u{0429}"] = 'SHT';
			$iso9_table["\u{0449}"] = 'sht';
			$iso9_table["\u{042A}"] = 'A';
			$iso9_table["\u{044A}"] = 'a';
			break;
		case 'uk':
		case 'uk_ua':
		case 'uk_UA':
			$iso9_table["\u{0418}"] = 'Y';
			$iso9_table["\u{0438}"] = 'y';
			break;
	}

	$is_term = false;
	$backtrace = debug_backtrace();
	foreach ( $backtrace as $backtrace_entry ) {
		if ( $backtrace_entry['function'] == 'wp_insert_term' ) {
			$is_term = true;
			break;
		}
	}

	$term = $is_term ? $wpdb->get_var("SELECT slug FROM {$wpdb->terms} WHERE name = '$title'") : '';
	if ( empty($term) ) {
		$title = strtr($title, apply_filters('ctl_table', $iso9_table));
		if (function_exists('iconv')){
			$title = iconv('UTF-8', 'UTF-8//TRANSLIT//IGNORE', $title);
		}
		$title = preg_replace("/[^A-Za-z0-9'_\-\.]/", '-', $title);
		$title = preg_replace('/\-+/', '-', $title);
		$title = preg_replace('/^-+/', '', $title);
		$title = preg_replace('/-+$/', '', $title);
	} else {
		$title = $term;
	}

	return $title;
}

Changelog

Since 1.2.0 Introduced.

Where the hook is called

sanitize_title()
sanitize_title
wp-includes/formatting.php 2244
$title = apply_filters( 'sanitize_title', $title, $raw_title, $context );

Where the hook is used in WordPress

wp-includes/default-filters.php 309
add_filter( 'sanitize_title', 'sanitize_title_with_dashes', 10, 3 );