WP_Http::make_absolute_url( string $maybe_relative_path, string $url ): string
- Since
- 3.4.0
- Source
wp-includes/class-wp-http.php:973
Description
If an Absolute URL is provided, no processing of that URL is done.
Compatibility
- WordPress
- since 3.4.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
$maybe_relative_pathstring- The URL which might be relative.
$urlstring- The URL which $maybe_relative_path is relative to.
Return value
string- An Absolute URL, in a failure condition where the URL cannot be parsed, the relative URL will be returned.
Performance profile
How much work a call to WP_Http::make_absolute_url() 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
- Light
- Scaling
- Scales with input
- Instructions
- 5–75
- Plugin surface
- None
- Called by
- 4
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 93.
Nothing here hands control to plugin code.
4 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_Http::make_absolute_url() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($url) | 5 | none |
!empty($url) | 10 | wp_parse_url() |
!empty($url) | 15–17 | wp_parse_url(), wp_parse_url() |
!empty($url) && empty($relative_url_parts) | 43–59 | wp_parse_url(), wp_parse_url(), ltrim() |
!empty($url) | 60–75 | wp_parse_url(), wp_parse_url(), strrpos(), ltrim() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 93 | 5–75 | 14 | |
| 8.5 | 93 | 5–75 | 14 | |
| 8.4 | 93 | 5–75 | 14 | 12 fewer instructions than PHP 8.3 |
| 8.3 | 105 | 5–84 | 14 | |
| 8.2 | 105 | 5–84 | 14 | |
| 8.1 | 105 | 5–84 | 14 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 106 | 5–85 | 14 |
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_parse_url()A wrapper for PHP's parse_url() function that handles consistency in the return values across PHP versions.
Used by · 4
- WP_Http::handle_redirects()Handles an HTTP redirect and follows it if appropriate.
- WP_REST_URL_Details_Controller::get_icon()Parses the site icon from the provided HTML.
- WP_REST_URL_Details_Controller::get_image()Parses the Open Graph (OG) Image from the provided HTML.
- _links_add_base()Callback to add a base URL to relative links in passed content.
Source code
public static function make_absolute_url( $maybe_relative_path, $url ) { if ( empty( $url ) ) { return $maybe_relative_path; } $url_parts = wp_parse_url( $url ); if ( ! $url_parts ) { return $maybe_relative_path; } $relative_url_parts = wp_parse_url( $maybe_relative_path ); if ( ! $relative_url_parts ) { return $maybe_relative_path; } // Check for a scheme on the 'relative' URL. if ( ! empty( $relative_url_parts['scheme'] ) ) { return $maybe_relative_path; } $absolute_path = $url_parts['scheme'] . '://'; // Schemeless URLs will make it this far, so we check for a host in the relative URL // and convert it to a protocol-URL. if ( isset( $relative_url_parts['host'] ) ) { $absolute_path .= $relative_url_parts['host']; if ( isset( $relative_url_parts['port'] ) ) { $absolute_path .= ':' . $relative_url_parts['port']; } } else { $absolute_path .= $url_parts['host']; if ( isset( $url_parts['port'] ) ) { $absolute_path .= ':' . $url_parts['port']; } } // Start off with the absolute URL path. $path = ! empty( $url_parts['path'] ) ? $url_parts['path'] : '/'; // If it's a root-relative path, then great. if ( ! empty( $relative_url_parts['path'] ) && '/' === $relative_url_parts['path'][0] ) { $path = $relative_url_parts['path']; // Else it's a relative path. } elseif ( ! empty( $relative_url_parts['path'] ) ) { // Strip off any file components from the absolute path. $path = substr( $path, 0, strrpos( $path, '/' ) + 1 ); // Build the new path. $path .= $relative_url_parts['path']; // Strip all /path/../ out of the path. while ( strpos( $path, '../' ) > 1 ) { $path = preg_replace( '![^/]+/\.\./!', '', $path ); } // Strip any final leading ../ from the path. $path = preg_replace( '!^/(\.\./)+!', '', $path ); } // Add the query string. if ( ! empty( $relative_url_parts['query'] ) ) { $path .= '?' . $relative_url_parts['query']; } // Add the fragment. if ( ! empty( $relative_url_parts['fragment'] ) ) { $path .= '#' . $relative_url_parts['fragment']; } return $absolute_path . '/' . ltrim( $path, '/' ); }Changelog
Introduced in 3.4.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/class-wp-http.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.