wp_normalize_path( string $path ): string
- Since
- 3.9.0, 4.4.0, 4.5.0, 4.9.7, 7.0.0
- Source
wp-includes/functions.php:2194
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.
Compatibility
- WordPress
- since 7.0.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$pathstring- Path to normalize.
Return value
string- Normalized path.
Performance profile
How much work a call to wp_normalize_path() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 9–44
- Plugin surface
- None
- Called by
- 26
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 47.
Nothing here hands control to plugin code.
26 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 5 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_normalize_path() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
isset($cache[$path]) | 9 | none |
!isset($cache[$path]) && !wp_is_stream() | 29 | wp_is_stream() |
!isset($cache[$path]) && !wp_is_stream() | 33 | wp_is_stream(), ucfirst() |
!isset($cache[$path]) && wp_is_stream() | 40 | wp_is_stream(), explode() |
!isset($cache[$path]) && wp_is_stream() | 44 | wp_is_stream(), explode(), ucfirst() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 47 | 9–44 | 3 | |
| 8.5 | 47 | 9–44 | 3 | |
| 8.4 | 47 | 9–44 | 3 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 56 | 9–53 | 3 | |
| 8.2 | 56 | 9–53 | 3 | |
| 8.1 | 56 | 9–53 | 3 | |
| 7.4 | 56 | 9–53 | 3 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 1
- wp_is_stream()Tests if a given path is a stream URL
Used by · 26
- Theme_Installer_Skin::do_overwrite()Checks if the theme can be overwritten and outputs the HTML for overwriting a theme on upload.
- WP_Block_Metadata_Registry::get_collection_block_metadata_files()Gets the list of absolute paths to all block metadata files that are part of the given collection.
- WP_Block_Metadata_Registry::get_default_collection_roots()Gets the default collection root directory paths.
- WP_Block_Metadata_Registry::get_metadata()Retrieves block metadata for a given block within a specific collection.
- WP_Block_Metadata_Registry::register_collection()Registers a block metadata collection.
- WP_Font_Collection::load_from_json()Loads font collection data from a JSON file or URL.
- WP_REST_Attachments_Controller::get_attachment_upload_subdir()Returns the uploads subdirectory an attachment is stored in.
- WP_Recovery_Mode::get_extension_for_error()Gets the extension that the error occurred in.
- _get_plugin_from_callback()Internal helper function to find the plugin from a meta box callback.
- _wp_connectors_resolve_ai_provider_logo_url()Resolves an AI provider logo file path to a URL.
- get_block_asset_url()Gets the URL to a block asset.
- plugin_basename()Gets the basename of a plugin.
Show all 26
- plugins_url()Retrieves a URL within the plugins or mu-plugins directory.
- register_block_script_handle()Finds a script handle for the selected block metadata field.
- register_block_script_module_id()Finds a script module ID for the selected block metadata field.
- register_block_style_handle()Finds a style handle for the block metadata field.
- register_block_type_from_metadata()Registers a block type from the metadata stored in the `block.json` file.
- register_core_block_style_handles()Registers core block style handles.
- validate_file()Validates a file name and path against an allowed set of rules.
- wp_debug_backtrace_summary()Returns a comma-separated string or array of functions that have been called to get to the current point in code.
- wp_delete_file_from_directory()Deletes a file if its path is within the given directory.
- wp_generate_block_templates_export_file()Creates an export of the current templates and template parts from the site editor at the specified path in a ZIP file.
- wp_json_file_decode()Reads and decodes a JSON file.
- wp_privacy_generate_personal_data_export_file()Generate the personal data export file.
- wp_register_plugin_realpath()Register a plugin's real path.
- wp_validate_redirect()Validates a URL for use in a redirect.
Source code
function wp_normalize_path( $path ): string { $path = (string) $path; static $cache = array(); if ( isset( $cache[ $path ] ) ) { return $cache[ $path ]; } $original_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 = (string) preg_replace( '|(?<=.)/+|', '/', $path ); // Windows paths should uppercase the drive letter. if ( ':' === substr( $path, 1, 1 ) ) { $path = ucfirst( $path ); } $cache[ $original_path ] = $wrapper . $path; return $cache[ $original_path ];}Changelog
Introduced in 3.9.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.