_wp_normalize_relative_css_links()WP 5.9.0

Makes URLs relative to the WordPress installation.

Internal function — this function is designed to be used by the kernel itself. It is not recommended to use this function in your code.

No Hooks.

Return

String. The CSS with URLs made relative to the WordPress installation.

Usage

_wp_normalize_relative_css_links( $css, $stylesheet_url );
$css(string) (required)
The CSS to make URLs relative to the WordPress installation.
$stylesheet_url(string) (required)
The URL to the stylesheet.

Changelog

Since 5.9.0 Introduced.

_wp_normalize_relative_css_links() code WP 6.5.2

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
	);
}