Title: WP_Theme_JSON::prepend_to_selector
Published: August 8, 2023
Last modified: August 20, 2026

---

# WP_Theme_JSON::prepend_to_selector( string $selector, string $to_prepend ): string

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/wp_theme_json/prepend_to_selector/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/classes/wp_theme_json/prepend_to_selector/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_theme_json/prepend_to_selector/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_theme_json/prepend_to_selector/?output_format=md#source)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_theme_json/prepend_to_selector/?output_format=md#changelog)

Prepends a sub-selector to an existing one.

## 󠀁[Description](https://developer.wordpress.org/reference/classes/wp_theme_json/prepend_to_selector/?output_format=md#description)󠁿

Given the compounded $selector “h1, h2, h3” and the $to_prepend selector “.some-
class ” the result will be “.some-class h1, .some-class h2, .some-class h3”.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_theme_json/prepend_to_selector/?output_format=md#parameters)󠁿

 `$selector`stringrequired

Original selector.

`$to_prepend`stringrequired

Selector to prepend.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_theme_json/prepend_to_selector/?output_format=md#return)󠁿

 string The new selector.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_theme_json/prepend_to_selector/?output_format=md#source)󠁿

    ```php
    protected static function prepend_to_selector( $selector, $to_prepend ) {
    	if ( ! str_contains( $selector, ',' ) ) {
    		return $to_prepend . trim( $selector, " \t\n" );
    	}

    	/**
    	 * Check for an opportunity to skip the more-costly selector splitting.
    	 * This should be possible if there are no comments, strings, functions,
    	 * URLs, escapes, or comment declaration openers (CDOs).
    	 *
    	 * Note that this means the fast-path will not apply for selectors like
    	 * the following incomplete list:
    	 *
    	 *  - `[class ~= "wide"]`
    	 *  - `.wp-block:is(.is-style-a, .is-style-b)`
    	 *  - `:nth-child(1)`
    	 *
    	 * These syntax forms all present opportunities where a comma may not
    	 * separate selectors. If none of the start characters are present,
    	 * there should be no way for a comma to mean anything other than a
    	 * comma token. The exception are syntax errors, which are not handled here.
    	 *
    	 * @see https://www.w3.org/TR/css-syntax-3/#parse-comma-separated-list-of-component-values
    	 */
    	if ( strlen( $selector ) === strcspn( $selector, '/\'"(<\\' ) ) {
    		return $to_prepend . preg_replace( '~[ \t\n]*,[ \t\n]*~', ", {$to_prepend}", trim( $selector, " \t\n" ) );
    	}

    	$new_selectors = array();
    	$selectors     = static::split_selector_list( $selector );
    	foreach ( $selectors as $sel ) {
    		$new_selectors[] = $to_prepend . $sel;
    	}
    	return implode( ', ', $new_selectors );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-theme-json.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/class-wp-theme-json.php#L1537)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/class-wp-theme-json.php#L1537-L1571)

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_theme_json/prepend_to_selector/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.3.0](https://developer.wordpress.org/reference/since/6.3.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_theme_json%2Fprepend_to_selector%2F)
before being able to contribute a note or feedback.