wppaste
WordPress

wp_guess_url(): string

Since
2.6.0
Source
wp-includes/functions.php:6350
Guesses the URL for the site.

Description

Will remove wp-admin links to retrieve only return URLs not in the wp-admin directory.

Compatibility

WordPress
since 2.6.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.

Return value

string
The guessed URL.

Performance profile

How much work a call to wp_guess_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
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
12–55

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
5

5 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 3 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_guess_url() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
defined('WP_SITEURL')12rtrim()
always31–53is_ssl(), rtrim()
$abspath_fix !== false && $abspath_fix51–55preg_quote(), is_ssl(), rtrim()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev8212–558
8.58212–558
8.48212–55845 fewer instructions than PHP 8.3
8.312712–828
8.212712–828
8.112712–8283 fewer instructions than PHP 7.4
7.413012–858

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 · 2

Used by · 5

Source code

function wp_guess_url() {	if ( defined( 'WP_SITEURL' ) && '' !== WP_SITEURL ) {		$url = WP_SITEURL;	} else {		$abspath_fix         = str_replace( '\\', '/', ABSPATH );		$script_filename_dir = dirname( $_SERVER['SCRIPT_FILENAME'] ); 		// The request is for the admin.		if ( str_contains( $_SERVER['REQUEST_URI'], 'wp-admin' ) || str_contains( $_SERVER['REQUEST_URI'], 'wp-login.php' ) ) {			$path = preg_replace( '#/(wp-admin/?.*|wp-login\.php.*)#i', '', $_SERVER['REQUEST_URI'] ); 			// The request is for a file in ABSPATH.		} elseif ( $script_filename_dir . '/' === $abspath_fix ) {			// Strip off any file/query params in the path.			$path = preg_replace( '#/[^/]*$#i', '', $_SERVER['PHP_SELF'] ); 		} else {			if ( str_contains( $_SERVER['SCRIPT_FILENAME'], $abspath_fix ) ) {				// Request is hitting a file inside ABSPATH.				$directory = str_replace( ABSPATH, '', $script_filename_dir );				// Strip off the subdirectory, and any file/query params.				$path = preg_replace( '#/' . preg_quote( $directory, '#' ) . '/[^/]*$#i', '', $_SERVER['REQUEST_URI'] );			} elseif ( str_contains( $abspath_fix, $script_filename_dir ) ) {				// Request is hitting a file above ABSPATH.				$subdirectory = substr( $abspath_fix, strpos( $abspath_fix, $script_filename_dir ) + strlen( $script_filename_dir ) );				// Strip off any file/query params from the path, appending the subdirectory to the installation.				$path = preg_replace( '#/[^/]*$#i', '', $_SERVER['REQUEST_URI'] ) . $subdirectory;			} else {				$path = $_SERVER['REQUEST_URI'];			}		} 		$schema = is_ssl() ? 'https://' : 'http://'; // set_url_scheme() is not defined yet.		$url    = $schema . $_SERVER['HTTP_HOST'] . $path;	} 	return rtrim( $url, '/' );}

Changelog

Introduced in 2.6.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/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.