WP_Theme_JSON::scope_selector() WordPress Method

The WP_Theme_JSON::scope_selector() method is used to return the CSS selector for the current theme. This is used by the wp_enqueue_scripts() function to add the theme's CSS to the page.

WP_Theme_JSON::scope_selector( string $scope, string $selector ) #

Function that scopes a selector with another one. This works a bit like SCSS nesting except the & operator isn’t supported.


Description

$scope = '.a, .b .c';
$selector = '> .x, .y';
$merged = scope_selector( $scope, $selector );
// $merged is '.a > .x, .a .y, .b .c > .x, .b .c .y'

Top ↑

Parameters

$scope

(string)(Required)Selector to scope to.

$selector

(string)(Required)Original selector.


Top ↑

Return

(string) Scoped selector.


Top ↑

Source

File: wp-includes/class-wp-theme-json.php

	protected static function scope_selector( $scope, $selector ) {
		$scopes    = explode( ',', $scope );
		$selectors = explode( ',', $selector );

		$selectors_scoped = array();
		foreach ( $scopes as $outer ) {
			foreach ( $selectors as $inner ) {
				$selectors_scoped[] = trim( $outer ) . ' ' . trim( $inner );
			}
		}

		return implode( ', ', $selectors_scoped );
	}

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.