_wp_relative_upload_path( string $path ): string
- Since
- 2.9.0
- Source
wp-includes/post.php:899
Description
The path is relative to the current upload dir.
Compatibility
- WordPress
- since 2.9.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- Full path to the file.
Return value
string- Relative path on success, unchanged path on failure.
Performance profile
How much work a call to _wp_relative_upload_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
- 14–23
- Plugin surface
- 1 hook
- Called by
- 6
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 23.
Third-party callbacks on '_wp_relative_upload_path' run inside this call, and their cost is not bounded by anything here.
6 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly
Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost _wp_relative_upload_path() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!$new_path | 14 | wp_get_upload_dir(), apply_filters() |
$new_path | 23 | wp_get_upload_dir(), ltrim(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 23 | 14–23 | 1 | |
| 8.5 | 23 | 14–23 | 1 | |
| 8.4 | 23 | 14–23 | 1 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 29 | 17–29 | 1 | |
| 8.2 | 29 | 17–29 | 1 | |
| 8.1 | 29 | 17–29 | 1 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 30 | 18–30 | 1 |
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.
Hooks and filters fired · 1
One hook fires while _wp_relative_upload_path() runs, in this order:
- apply_filters( _wp_relative_upload_path )filterline 916 (+17 into the body)
Filters the relative path to an uploaded file.
Uses · 3
- wp_get_upload_dir()Retrieves uploads directory information.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 6
- WP_REST_Attachments_Controller::edit_media_item()Applies edits to a media item and creates a new attachment record.
- _wp_image_meta_replace_original()Updates the attached file and image meta data when the original image was edited.
- update_attached_file()Updates attachment file path based on attachment ID.
- wp_create_image_subsizes()Creates image sub-sizes, adds the new data to the image meta `sizes` array, and updates the image metadata.
- wp_restore_image()Restores the metadata for a given attachment.
- wp_save_image()Saves image to post, along with enqueued changes in `$_REQUEST['history']`.
Source code
function _wp_relative_upload_path( $path ) { $new_path = $path; $uploads = wp_get_upload_dir(); if ( str_starts_with( $new_path, $uploads['basedir'] ) ) { $new_path = str_replace( $uploads['basedir'], '', $new_path ); $new_path = ltrim( $new_path, '/' ); } /** * Filters the relative path to an uploaded file. * * @since 2.9.0 * * @param string $new_path Relative path to the file. * @param string $path Full path to the file. */ return apply_filters( '_wp_relative_upload_path', $new_path, $path );}Changelog
Introduced in 2.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 6.8.8 tag, from
src/wp-includes/post.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.