Title: WP_Theme_JSON::prepend_to_selector
Published: August 8, 2023
Last modified: February 24, 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)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_theme_json/prepend_to_selector/?output_format=md#wp--skip-link--target)

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 . $selector;
    	}
    	$new_selectors = array();
    	$selectors     = explode( ',', $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/6.9.4/src/wp-includes/class-wp-theme-json.php#L1118)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/class-wp-theme-json.php#L1118-L1128)

## 󠀁[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.