Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

_wp_normalize_relative_css_links() WordPress Function

The function _wp_normalize_relative_css_links() is used to normalize relative URLs in CSS files. This is done by converting all relative URLs to absolute URLs. This ensures that all CSS files will load correctly regardless of the server's configuration.

_wp_normalize_relative_css_links( string $css, string $stylesheet_url ) #

Makes URLs relative to the WordPress installation.


Parameters

$css

(string)(Required)The CSS to make URLs relative to the WordPress installation.

$stylesheet_url

(string)(Required)The URL to the stylesheet.


Top ↑

Return

(string) The CSS with URLs made relative to the WordPress installation.


Top ↑

Source

File: wp-includes/script-loader.php

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


Top ↑

Changelog

Changelog
VersionDescription
5.9.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.