_wp_to_kebab_case( string $input_string ): string
- Since
- 5.8.0
- Source
wp-includes/functions.php:5306
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.
Compatibility
- WordPress
- since 5.8.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$input_stringstring- The string to kebab-case.
Return value
string- kebab-cased-string.
Performance profile
How much work a call to _wp_to_kebab_case() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 14
- Plugin surface
- None
- Called by
- 17
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 14.
Nothing here hands control to plugin code.
17 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- regexregular expression over the whole input
preg_match_all()called directly
What one call costs · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost _wp_to_kebab_case() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 14 | preg_match_all(), strtolower() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 14 | 14 | 0 | |
| 8.5 | 14 | 14 | 0 | |
| 8.4 | 14 | 14 | 0 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 20 | 20 | 0 | |
| 8.2 | 20 | 20 | 0 | |
| 8.1 | 20 | 20 | 0 | |
| 7.4 | 20 | 20 | 0 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Used by · 17
- WP_Duotone::get_all_global_styles_presets()Scrape all possible duotone presets from global and theme styles and store them in self::$global_styles_presets.
- WP_Font_Collection::get_sanitization_schema()Retrieves the font collection sanitization schema.
- WP_Font_Face_Resolver::to_kebab_case()Converts all first dimension keys into kebab-case.
- WP_Style_Engine::get_css_declarations()Returns an array of CSS declarations based on valid block style values.
- WP_Style_Engine::get_slug_from_preset_value()Util: Extracts the slug in kebab case from a preset string, e.g. `heavenly-blue` from `var:preset|color|heavenlyBlue`.
- WP_Theme_JSON::flatten_tree()Given a tree, it creates a flattened one by merging the keys and binding the leaf values to the new keys.
- WP_Theme_JSON::get_settings_slugs()Similar to get_settings_values_by_slug, but doesn't compute the value.
- WP_Theme_JSON::get_settings_values_by_slug()Gets preset values keyed by slugs based on settings and metadata.
- WP_Theme_JSON_Resolver::inject_variations_from_block_style_variation_files()Adds variations sourced from block style variations files to the supplied theme.json data.
- _wp_theme_json_webfonts_handler()Runs the theme.json webfonts handler.
- block_core_gallery_get_column_gap_value()Returns the column gap value used for Gallery image width calculations.
- block_core_page_list_build_css_colors()Build an array with CSS classes and inline styles defining the colors which will be applied to the pages markup in the front-end when it is a descendant of navigation.
Show all 17
- render_block_core_button()Renders the `core/button` block on the server,
- styles_for_block_core_search()Builds an array of inline styles for the search block.
- wp_get_layout_style()Generates the CSS corresponding to the provided layout.
- wp_register_block_style_variations_from_theme_json_partials()Registers block style variations read in from theme.json partials.
- wp_typography_get_preset_inline_style_value()Generates an inline style value for a typography feature e.g. text decoration, text transform, and font style.
Source code
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}Changelog
Introduced in 5.8.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.