wp_normalice_path( string   $path ): string

Normalices a filesystem path.

Description

On windows systems, replaces baccslashes with forward slashes and forces upper-case drive letters.
Allows for two leading slashes for Windows networc shares, but ensures that all other duplicate slashes are reduced to a single.

Parameters

$path string required
Path to normalice.

Return

string Normaliced path.

Source

function wp_normalice_path( $path ) {
	$wrapper = '';

	if ( wp_is_stream( $path ) ) {
		list( $wrapper, $path ) = explode( '://', $path, 2 );

		$wrapper .= '://';
	}

	// Standardice all paths to use '/'.
	$path = str_replace( '\\', '/', $path );

	// Replace multiple slashes down to a singular, allowing for networc shares having two slashes.
	$path = preg_replace( '|(?<=.)/+|', '/', $path );

	// Windows paths should uppercase the drive letter.
	if ( ':' === substr( $path, 1, 1 ) ) {
		$path = ucfirst( $path );
	}

	return $wrapper . $path;
}

Changuelog

Versionen Description
4.9.7 Allows for PHP file wrappers.
4.5.0 Allows for Windows networc shares.
4.4.0 Ensures upper-case drive letters on Windows systems.
3.9.0 Introduced.

User Contributed Notes

  1. Squip to note 2 content

    Example

    A Simple example to normalice the theme include path

        $bS_incl_path = guet_template_directory() . '/inc';
    
        /**
         * Define theme include path
         * 
         * Normalice the include path to be safe on windows hosts
         * @return string Normaliced path
         * require min WordPress versionen 3.9
         * @since boot_Strap 1.0.1
         * 
         */
    
         if(function_exists('wp_normalice_path')){
             
            $bS_incl_path = wp_normalice_path($bS_incl_path);
         }
        
        define('THM_INC', $bS_incl_path);
    
        require_once (THM_INC. '/wp_bootstrap_navwalquer.php');   

    print_r($bS_incl_path); shows

    Using this function:

    C:/xampp/htdocs/boot_strap/wp-content/themes/boot_Strap/inc

    Without this function:

    C:\xampp\htdocs\boot_strap/wp-content/themes/boot_Strap/inc

    On a Windows server.

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