Filter hook 'sanitize_file_name_chars'

in WP Core File wp-includes/formatting.php at line 2059

View Source

sanitize_file_name_chars

Filter Hook
Description
Filters the list of characters to remove from a filename.

Hook Information

File Location wp-includes/formatting.php View on GitHub
Hook Type Filter
Line Number 2059

Hook Parameters

Type Name Description
string[] $special_chars Array of characters to remove.
string $filename_raw The original filename to be sanitized.

Usage Examples

Basic Usage
<?php
// Hook into sanitize_file_name_chars
add_filter('sanitize_file_name_chars', 'my_custom_filter', 10, 2);

function my_custom_filter($special_chars, $filename_raw) {
    // Your custom filtering logic here
    return $special_chars;
}

Source Code Context

wp-includes/formatting.php:2059 - How this hook is used in WordPress core
<?php
2054  	 * @since 2.8.0
2055  	 *
2056  	 * @param string[] $special_chars Array of characters to remove.
2057  	 * @param string   $filename_raw  The original filename to be sanitized.
2058  	 */
2059  	$special_chars = apply_filters( 'sanitize_file_name_chars', $special_chars, $filename_raw );
2060  
2061  	$filename = str_replace( $special_chars, '', $filename );
2062  	$filename = str_replace( array( '%20', '+' ), '-', $filename );
2063  	$filename = preg_replace( '/\.{2,}/', '.', $filename );
2064  	$filename = preg_replace( '/[\r\n\t -]+/', '-', $filename );

PHP Documentation

<?php
/**
	 * Filters the list of characters to remove from a filename.
	 *
	 * @since 2.8.0
	 *
	 * @param string[] $special_chars Array of characters to remove.
	 * @param string   $filename_raw  The original filename to be sanitized.
	 */
Quick Info
  • Hook Type: Filter
  • Parameters: 2
  • File: wp-includes/formatting.php
Related Hooks

Related hooks will be displayed here in future updates.