Title: wp_guess_url
Published: April 25, 2014
Last modified: February 24, 2026

---

# wp_guess_url(): string

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_guess_url/?output_format=md#description)
 * [Return](https://developer.wordpress.org/reference/functions/wp_guess_url/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_guess_url/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_guess_url/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_guess_url/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_guess_url/?output_format=md#wp--skip-link--target)

Guesses the URL for the site.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_guess_url/?output_format=md#description)󠁿

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

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_guess_url/?output_format=md#return)󠁿

 string The guessed URL.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_guess_url/?output_format=md#source)󠁿

    ```php
    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, '/' );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/functions.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/functions.php#L6311)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/functions.php#L6311-L6348)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_guess_url/?output_format=md#related)󠁿

| Uses | Description | 
| [is_ssl()](https://developer.wordpress.org/reference/functions/is_ssl/)`wp-includes/load.php` |

Determines if SSL is used.

  |

| Used by | Description | 
| [populate_options()](https://developer.wordpress.org/reference/functions/populate_options/)`wp-admin/includes/schema.php` |

Create WordPress options and set the default values.

  | 
| [wp_install()](https://developer.wordpress.org/reference/functions/wp_install/)`wp-admin/includes/upgrade.php` |

Installs the site.

  | 
| [wp_not_installed()](https://developer.wordpress.org/reference/functions/wp_not_installed/)`wp-includes/load.php` |

Redirects to the installer if WordPress is not installed.

  | 
| [wp_default_styles()](https://developer.wordpress.org/reference/functions/wp_default_styles/)`wp-includes/script-loader.php` |

Assigns default styles to $styles object.

  | 
| [wp_default_scripts()](https://developer.wordpress.org/reference/functions/wp_default_scripts/)`wp-includes/script-loader.php` |

Registers all WordPress scripts.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_guess_url/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.6.0](https://developer.wordpress.org/reference/since/2.6.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_guess_url%2F)
before being able to contribute a note or feedback.