wppaste
WordPress

WP_Http::make_absolute_url( string $maybe_relative_path, string $url ): string

Since
3.4.0
Source
wp-includes/class-wp-http.php:973
Converts a relative URL to an absolute URL relative to a given URL.

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

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
5–75

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 93.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
4

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.

WhenInstructionsCalls it makes
empty($url)5none
!empty($url)10wp_parse_url()
!empty($url)15–17wp_parse_url(), wp_parse_url()
!empty($url) && empty($relative_url_parts)43–59wp_parse_url(), wp_parse_url(), ltrim()
!empty($url)60–75wp_parse_url(), wp_parse_url(), strrpos(), ltrim()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev935–7514
8.5935–7514
8.4935–751412 fewer instructions than PHP 8.3
8.31055–8414
8.21055–8414
8.11055–84141 fewer instruction than PHP 7.4
7.41065–8514

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

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 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.