wp_guess_url

The timeline below displays how wordpress function wp_guess_url has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Guesses the URL for the site.
 *
 * Will remove wp-admin links to retrieve only return URLs not in the wp-admin
 * directory.
 *
 * @since 2.6.0
 *
 * @return string The guessed URL.
 */
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, '/');
}

WordPress Version: 6.1

/**
 * Guesses the URL for the site.
 *
 * Will remove wp-admin links to retrieve only return URLs not in the wp-admin
 * directory.
 *
 * @since 2.6.0
 *
 * @return string The guessed URL.
 */
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 (strpos($_SERVER['REQUEST_URI'], 'wp-admin') !== false || strpos($_SERVER['REQUEST_URI'], 'wp-login.php') !== false) {
            $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 (false !== strpos($_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 (false !== strpos($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, '/');
}

WordPress Version: 5.5

/**
 * Guess the URL for the site.
 *
 * Will remove wp-admin links to retrieve only return URLs not in the wp-admin
 * directory.
 *
 * @since 2.6.0
 *
 * @return string The guessed URL.
 */
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 (strpos($_SERVER['REQUEST_URI'], 'wp-admin') !== false || strpos($_SERVER['REQUEST_URI'], 'wp-login.php') !== false) {
            $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 (false !== strpos($_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 (false !== strpos($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, '/');
}

WordPress Version: 5.4

/**
 * Guess the URL for the site.
 *
 * Will remove wp-admin links to retrieve only return URLs not in the wp-admin
 * directory.
 *
 * @since 2.6.0
 *
 * @return string The guessed URL.
 */
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 (strpos($_SERVER['REQUEST_URI'], 'wp-admin') !== false || strpos($_SERVER['REQUEST_URI'], 'wp-login.php') !== false) {
            $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 (false !== strpos($_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 (false !== strpos($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, '/');
}

WordPress Version: 4.9

/**
 * Guess the URL for the site.
 *
 * Will remove wp-admin links to retrieve only return URLs not in the wp-admin
 * directory.
 *
 * @since 2.6.0
 *
 * @return string The guessed URL.
 */
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 (strpos($_SERVER['REQUEST_URI'], 'wp-admin') !== false || strpos($_SERVER['REQUEST_URI'], 'wp-login.php') !== false) {
            $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 (false !== strpos($_SERVER['SCRIPT_FILENAME'], $abspath_fix)) {
            // Request is hitting a file inside ABSPATH
            $directory = str_replace(ABSPATH, '', $script_filename_dir);
            // Strip off the sub directory, and any file/query params
            $path = preg_replace('#/' . preg_quote($directory, '#') . '/[^/]*$#i', '', $_SERVER['REQUEST_URI']);
        } elseif (false !== strpos($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 sub directory 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, '/');
}

WordPress Version: 4.3

/**
 * Guess the URL for the site.
 *
 * Will remove wp-admin links to retrieve only return URLs not in the wp-admin
 * directory.
 *
 * @since 2.6.0
 *
 * @return string The guessed URL.
 */
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 (strpos($_SERVER['REQUEST_URI'], 'wp-admin') !== false || strpos($_SERVER['REQUEST_URI'], 'wp-login.php') !== false) {
            $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 (false !== strpos($_SERVER['SCRIPT_FILENAME'], $abspath_fix)) {
            // Request is hitting a file inside ABSPATH
            $directory = str_replace(ABSPATH, '', $script_filename_dir);
            // Strip off the sub directory, and any file/query params
            $path = preg_replace('#/' . preg_quote($directory, '#') . '/[^/]*$#i', '', $_SERVER['REQUEST_URI']);
        } elseif (false !== strpos($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 sub directory to the install
            $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, '/');
}

WordPress Version: 4.0

/**
 * Guess the URL for the site.
 *
 * Will remove wp-admin links to retrieve only return URLs not in the wp-admin
 * directory.
 *
 * @since 2.6.0
 *
 * @return string The guessed URL.
 */
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 (strpos($_SERVER['REQUEST_URI'], 'wp-admin') !== false || strpos($_SERVER['REQUEST_URI'], 'wp-login.php') !== false) {
            $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 (false !== strpos($_SERVER['SCRIPT_FILENAME'], $abspath_fix)) {
            // Request is hitting a file inside ABSPATH
            $directory = str_replace(ABSPATH, '', $script_filename_dir);
            // Strip off the sub directory, and any file/query paramss
            $path = preg_replace('#/' . preg_quote($directory, '#') . '/[^/]*$#i', '', $_SERVER['REQUEST_URI']);
        } elseif (false !== strpos($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 sub directory to the install
            $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, '/');
}

WordPress Version: 3.8

/**
 * Guess the URL for the site.
 *
 * Will remove wp-admin links to retrieve only return URLs not in the wp-admin
 * directory.
 *
 * @since 2.6.0
 *
 * @return string
 */
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 (strpos($_SERVER['REQUEST_URI'], 'wp-admin') !== false || strpos($_SERVER['REQUEST_URI'], 'wp-login.php') !== false) {
            $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 (false !== strpos($_SERVER['SCRIPT_FILENAME'], $abspath_fix)) {
            // Request is hitting a file inside ABSPATH
            $directory = str_replace(ABSPATH, '', $script_filename_dir);
            // Strip off the sub directory, and any file/query paramss
            $path = preg_replace('#/' . preg_quote($directory, '#') . '/[^/]*$#i', '', $_SERVER['REQUEST_URI']);
        } elseif (false !== strpos($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 sub directory to the install
            $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, '/');
}

WordPress Version: 3.7

/**
 * Guess the URL for the site.
 *
 * Will remove wp-admin links to retrieve only return URLs not in the wp-admin
 * directory.
 *
 * @since 2.6.0
 *
 * @return string
 */
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 (strpos($_SERVER['REQUEST_URI'], 'wp-admin') !== false || strpos($_SERVER['REQUEST_URI'], 'wp-login.php') !== false) {
            $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 (false !== strpos($_SERVER['SCRIPT_FILENAME'], $abspath_fix)) {
            // Request is hitting a file inside ABSPATH
            $directory = str_replace(ABSPATH, '', $script_filename_dir);
            // Strip off the sub directory, and any file/query paramss
            $path = preg_replace('#/' . preg_quote($directory, '#') . '/[^/]*$#i', '', $_SERVER['REQUEST_URI']);
        } elseif (false !== strpos($abspath_fix, $script_filename_dir)) {
            // Request is hitting a file above ABSPATH
            $subdirectory = str_replace($script_filename_dir, '', $abspath_fix);
            // Strip off any file/query params from the path, appending the sub directory to the install
            $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, '/');
}