Title: _wp_to_kebab_case
Published: July 20, 2021
Last modified: August 20, 2026

---

# _wp_to_kebab_case( string $input_string ): string

## In this article

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

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

This function is trying to replicate what lodash’s kebabCase (JS library) does in
the client.

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

The reason we need this function is that we do some processing in both the client
and the server (e.g.: we generate preset classes from preset slugs) that needs to
create the same output.

We can’t remove or update the client’s library due to backward compatibility (some
of the output of lodash’s kebabCase is saved in the post content).
We have to make
the server behave like the client.

Changes to this function should follow updates in the client with the same logic.

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

 `$input_string`stringrequired

The string to kebab-case.

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

 string kebab-cased-string.

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

    ```php
    function _wp_to_kebab_case( $input_string ) {
    	// Ignore the camelCase names for variables so the names are the same as lodash so comparing and porting new changes is easier.
    	// phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase

    	/*
    	 * Some notable things we've removed compared to the lodash version are:
    	 *
    	 * - non-alphanumeric characters: rsAstralRange, rsEmoji, etc
    	 * - the groups that processed the apostrophe, as it's removed before passing the string to preg_match: rsApos, rsOptContrLower, and rsOptContrUpper
    	 *
    	 */

    	/** Used to compose unicode character classes. */
    	$rsLowerRange       = 'a-z\\xdf-\\xf6\\xf8-\\xff';
    	$rsNonCharRange     = '\\x00-\\x2f\\x3a-\\x40\\x5b-\\x60\\x7b-\\xbf';
    	$rsPunctuationRange = '\\x{2000}-\\x{206f}';
    	$rsSpaceRange       = ' \\t\\x0b\\f\\xa0\\x{feff}\\n\\r\\x{2028}\\x{2029}\\x{1680}\\x{180e}\\x{2000}\\x{2001}\\x{2002}\\x{2003}\\x{2004}\\x{2005}\\x{2006}\\x{2007}\\x{2008}\\x{2009}\\x{200a}\\x{202f}\\x{205f}\\x{3000}';
    	$rsUpperRange       = 'A-Z\\xc0-\\xd6\\xd8-\\xde';
    	$rsBreakRange       = $rsNonCharRange . $rsPunctuationRange . $rsSpaceRange;

    	/** Used to compose unicode capture groups. */
    	$rsBreak  = '[' . $rsBreakRange . ']';
    	$rsDigits = '\\d+'; // The last lodash version in GitHub uses a single digit here and expands it when in use.
    	$rsLower  = '[' . $rsLowerRange . ']';
    	$rsMisc   = '[^' . $rsBreakRange . $rsDigits . $rsLowerRange . $rsUpperRange . ']';
    	$rsUpper  = '[' . $rsUpperRange . ']';

    	/** Used to compose unicode regexes. */
    	$rsMiscLower = '(?:' . $rsLower . '|' . $rsMisc . ')';
    	$rsMiscUpper = '(?:' . $rsUpper . '|' . $rsMisc . ')';
    	$rsOrdLower  = '\\d*(?:1st|2nd|3rd|(?![123])\\dth)(?=\\b|[A-Z_])';
    	$rsOrdUpper  = '\\d*(?:1ST|2ND|3RD|(?![123])\\dTH)(?=\\b|[a-z_])';

    	$regexp = '/' . implode(
    		'|',
    		array(
    			$rsUpper . '?' . $rsLower . '+' . '(?=' . implode( '|', array( $rsBreak, $rsUpper, '$' ) ) . ')',
    			$rsMiscUpper . '+' . '(?=' . implode( '|', array( $rsBreak, $rsUpper . $rsMiscLower, '$' ) ) . ')',
    			$rsUpper . '?' . $rsMiscLower . '+',
    			$rsUpper . '+',
    			$rsOrdUpper,
    			$rsOrdLower,
    			$rsDigits,
    		)
    	) . '/u';

    	preg_match_all( $regexp, str_replace( "'", '', $input_string ), $matches );
    	return strtolower( implode( '-', $matches[0] ) );
    	// phpcs:enable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
    }
    ```

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

## 󠀁[Related](https://developer.wordpress.org/reference/functions/_wp_to_kebab_case/?output_format=md#related)󠁿

| Used by | Description | 
| [WP_Theme_JSON_Resolver::inject_variations_from_block_style_variation_files()](https://developer.wordpress.org/reference/classes/wp_theme_json_resolver/inject_variations_from_block_style_variation_files/)`wp-includes/class-wp-theme-json-resolver.php` |

Adds variations sourced from block style variations files to the supplied theme.json data.

  | 
| [WP_Font_Collection::get_sanitization_schema()](https://developer.wordpress.org/reference/classes/wp_font_collection/get_sanitization_schema/)`wp-includes/fonts/class-wp-font-collection.php` |

Retrieves the font collection sanitization schema.

  | 
| [WP_Font_Face_Resolver::to_kebab_case()](https://developer.wordpress.org/reference/classes/wp_font_face_resolver/to_kebab_case/)`wp-includes/fonts/class-wp-font-face-resolver.php` |

Converts all first dimension keys into kebab-case.

  | 
| [WP_Duotone::get_all_global_styles_presets()](https://developer.wordpress.org/reference/classes/wp_duotone/get_all_global_styles_presets/)`wp-includes/class-wp-duotone.php` |

Scrape all possible duotone presets from global and theme styles and store them in self::$global_styles_presets.

  | 
| [WP_Style_Engine::get_css_declarations()](https://developer.wordpress.org/reference/classes/wp_style_engine/get_css_declarations/)`wp-includes/style-engine/class-wp-style-engine.php` |

Returns an array of CSS declarations based on valid block style values.

  | 
| [WP_Style_Engine::get_slug_from_preset_value()](https://developer.wordpress.org/reference/classes/wp_style_engine/get_slug_from_preset_value/)`wp-includes/style-engine/class-wp-style-engine.php` |

Util: Extracts the slug in kebab case from a preset string, e.g. `heavenly-blue` from `var:preset|color|heavenlyBlue`.

  | 
| [_wp_theme_json_webfonts_handler()](https://developer.wordpress.org/reference/functions/_wp_theme_json_webfonts_handler/)`wp-includes/deprecated.php` |

Runs the theme.json webfonts handler.

  | 
| [WP_Theme_JSON::get_settings_values_by_slug()](https://developer.wordpress.org/reference/classes/wp_theme_json/get_settings_values_by_slug/)`wp-includes/class-wp-theme-json.php` |

Gets preset values keyed by slugs based on settings and metadata.

  | 
| [WP_Theme_JSON::get_settings_slugs()](https://developer.wordpress.org/reference/classes/wp_theme_json/get_settings_slugs/)`wp-includes/class-wp-theme-json.php` |

Similar to get_settings_values_by_slug, but doesn’t compute the value.

  | 
| [WP_Theme_JSON::flatten_tree()](https://developer.wordpress.org/reference/classes/wp_theme_json/flatten_tree/)`wp-includes/class-wp-theme-json.php` |

Given a tree, it creates a flattened one by merging the keys and binding the leaf values to the new keys.

  |

[Show 5 more](https://developer.wordpress.org/reference/functions/_wp_to_kebab_case/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/_wp_to_kebab_case/?output_format=md#)

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

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

## User Contributed Notes

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