_wp_normalize_relative_css_links

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

WordPress Version: 2.1

/**
 * Makes URLs relative to the WordPress installation.
 *
 * @since 5.9.0
 * @access private
 *
 * @param string $css            The CSS to make URLs relative to the WordPress installation.
 * @param string $stylesheet_url The URL to the stylesheet.
 *
 * @return string The CSS with URLs made relative to the WordPress installation.
 */
function _wp_normalize_relative_css_links($css, $stylesheet_url)
{
    return preg_replace_callback('#(url\s*\(\s*[\'"]?\s*)([^\'"\)]+)#', static function ($matches) use ($stylesheet_url) {
        list(, $prefix, $url) = $matches;
        // Short-circuit if the URL does not require normalization.
        if (str_starts_with($url, 'http:') || str_starts_with($url, 'https:') || str_starts_with($url, '//') || str_starts_with($url, '#') || str_starts_with($url, 'data:')) {
            return $matches[0];
        }
        // Build the absolute URL.
        $absolute_url = dirname($stylesheet_url) . '/' . $url;
        $absolute_url = str_replace('/./', '/', $absolute_url);
        // Convert to URL related to the site root.
        $url = wp_make_link_relative($absolute_url);
        return $prefix . $url;
    }, $css);
}

WordPress Version: 6.1

/**
 * Makes URLs relative to the WordPress installation.
 *
 * @since 5.9.0
 * @access private
 *
 * @param string $css            The CSS to make URLs relative to the WordPress installation.
 * @param string $stylesheet_url The URL to the stylesheet.
 *
 * @return string The CSS with URLs made relative to the WordPress installation.
 */
function _wp_normalize_relative_css_links($css, $stylesheet_url)
{
    $has_src_results = preg_match_all('#url\s*\(\s*[\'"]?\s*([^\'"\)]+)#', $css, $src_results);
    if ($has_src_results) {
        // Loop through the URLs to find relative ones.
        foreach ($src_results[1] as $src_index => $src_result) {
            // Skip if this is an absolute URL.
            if (0 === strpos($src_result, 'http') || 0 === strpos($src_result, '//')) {
                continue;
            }
            // Skip if the URL is an HTML ID.
            if (str_starts_with($src_result, '#')) {
                continue;
            }
            // Skip if the URL is a data URI.
            if (str_starts_with($src_result, 'data:')) {
                continue;
            }
            // Build the absolute URL.
            $absolute_url = dirname($stylesheet_url) . '/' . $src_result;
            $absolute_url = str_replace('/./', '/', $absolute_url);
            // Convert to URL related to the site root.
            $relative_url = wp_make_link_relative($absolute_url);
            // Replace the URL in the CSS.
            $css = str_replace($src_results[0][$src_index], str_replace($src_result, $relative_url, $src_results[0][$src_index]), $css);
        }
    }
    return $css;
}

WordPress Version: 9.1

/**
 * Make URLs relative to the WordPress installation.
 *
 * @since 5.9.0
 * @access private
 *
 * @param string $css            The CSS to make URLs relative to the WordPress installation.
 * @param string $stylesheet_url The URL to the stylesheet.
 *
 * @return string The CSS with URLs made relative to the WordPress installation.
 */
function _wp_normalize_relative_css_links($css, $stylesheet_url)
{
    $has_src_results = preg_match_all('#url\s*\(\s*[\'"]?\s*([^\'"\)]+)#', $css, $src_results);
    if ($has_src_results) {
        // Loop through the URLs to find relative ones.
        foreach ($src_results[1] as $src_index => $src_result) {
            // Skip if this is an absolute URL.
            if (0 === strpos($src_result, 'http') || 0 === strpos($src_result, '//')) {
                continue;
            }
            // Skip if the URL is an HTML ID.
            if (str_starts_with($src_result, '#')) {
                continue;
            }
            // Skip if the URL is a data URI.
            if (str_starts_with($src_result, 'data:')) {
                continue;
            }
            // Build the absolute URL.
            $absolute_url = dirname($stylesheet_url) . '/' . $src_result;
            $absolute_url = str_replace('/./', '/', $absolute_url);
            // Convert to URL related to the site root.
            $relative_url = wp_make_link_relative($absolute_url);
            // Replace the URL in the CSS.
            $css = str_replace($src_results[0][$src_index], str_replace($src_result, $relative_url, $src_results[0][$src_index]), $css);
        }
    }
    return $css;
}

WordPress Version: 5.9

/**
 * Make URLs relative to the WordPress installation.
 *
 * @since 5.9.0
 * @access private
 *
 * @param string $css            The CSS to make URLs relative to the WordPress installation.
 * @param string $stylesheet_url The URL to the stylesheet.
 *
 * @return string The CSS with URLs made relative to the WordPress installation.
 */
function _wp_normalize_relative_css_links($css, $stylesheet_url)
{
    $has_src_results = preg_match_all('#url\s*\(\s*[\'"]?\s*([^\'"\)]+)#', $css, $src_results);
    if ($has_src_results) {
        // Loop through the URLs to find relative ones.
        foreach ($src_results[1] as $src_index => $src_result) {
            // Skip if this is an absolute URL.
            if (0 === strpos($src_result, 'http') || 0 === strpos($src_result, '//')) {
                continue;
            }
            // Build the absolute URL.
            $absolute_url = dirname($stylesheet_url) . '/' . $src_result;
            $absolute_url = str_replace('/./', '/', $absolute_url);
            // Convert to URL related to the site root.
            $relative_url = wp_make_link_relative($absolute_url);
            // Replace the URL in the CSS.
            $css = str_replace($src_results[0][$src_index], str_replace($src_result, $relative_url, $src_results[0][$src_index]), $css);
        }
    }
    return $css;
}