_wp_normalize_relative_css_links

函数
_wp_normalize_relative_css_links ( $css, $stylesheet_url )
Access
Private
参数
  • (string) $css The CSS to make URLs relative to the WordPress installation.
    Required:
  • (string) $stylesheet_url The URL to the stylesheet.
    Required:
返回值
  • (string) The CSS with URLs made relative to the WordPress installation.
定义位置
相关方法
wp_kses_normalize_entitieswp_normalize_pathwp_make_link_relativewp_remote_retrieve_cookieswp_normalize_site_data
引入
5.9.0
弃用
-

_wp_normalize_relative_css_links: 这个函数将相对CSS链接规范化,以使用网站的主页URL。

使URL与WordPress的安装相对应。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function _wp_normalize_relative_css_links( $css, $stylesheet_url ) {
$has_src_results = preg_match_all( '#urls*(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;
}
function _wp_normalize_relative_css_links( $css, $stylesheet_url ) { $has_src_results = preg_match_all( '#urls*(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; }
function _wp_normalize_relative_css_links( $css, $stylesheet_url ) {
	$has_src_results = preg_match_all( '#urls*(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;
}

常见问题

FAQs
查看更多 >