wp_normalize_path() WordPress Function

The wp_normalize_path() function is used to fix the slashes in a path. It replaces backslashes with forward slashes and adds a trailing slash, if it is missing. This function is useful when you need to make sure that a path is consistent.

wp_normalize_path( string $path ) #

Normalize a filesystem path.


Description

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


Top ↑

Parameters

$path

(string)(Required)Path to normalize.


Top ↑

Return

(string) Normalized path.


Top ↑

Source

File: wp-includes/functions.php

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

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

		$wrapper .= '://';
	}

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

	// Replace multiple slashes down to a singular, allowing for network 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;
}


Top ↑

Changelog

Changelog
VersionDescription
4.9.7Allows for PHP file wrappers.
4.5.0Allows for Windows network shares.
4.4.0Ensures upper-case drive letters on Windows systems.
3.9.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More