sanitize_title_with_dashes( string $title, string $raw_title = '', string $context = 'display' ): string
- Since
- 1.2.0
- Source
wp-includes/formatting.php:2271
Description
Limits the output to alphanumeric characters, underscore (_) and dash (-).
Whitespace becomes a dash.
Compatibility
- WordPress
- since 1.2.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
$titlestring- The title to be sanitized.
$raw_titlestringoptional- Not used. Default empty.Default:
'' $contextstringoptional- The operation for which the string is sanitized.
When set to 'save', additional entities are converted to hyphens or stripped entirely. Default 'display'.Default:'display'
Return value
string- The sanitized title.
Performance profile
How much work a call to sanitize_title_with_dashes() 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
- 44–72
- Plugin surface
- None
- Called by
- 7
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, depending on the branch taken. The body compiles to 72.
Nothing here hands control to plugin code.
7 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost sanitize_title_with_dashes() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!seems_utf8() | 44–62 | strip_tags(), seems_utf8(), strtolower() |
seems_utf8() | 54–72 | strip_tags(), seems_utf8(), mb_strtolower(), utf8_uri_encode(), strtolower() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 76 | 44–76 | 3 | 4 more instructions than PHP 8.5 |
| 8.5 | 72 | 44–72 | 2 | |
| 8.4 | 72 | 44–72 | 2 | 45 fewer instructions than PHP 8.3 |
| 8.3 | 117 | 71–117 | 2 | |
| 8.2 | 117 | 71–117 | 2 | |
| 8.1 | 117 | 71–117 | 2 | |
| 7.4 | 117 | 71–117 | 2 |
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.
Uses · 2
- seems_utf8()Checks to see if a string is utf8 encoded.
- utf8_uri_encode()Encodes the Unicode values to be used in the URI.
Used by · 7
- WP_Plugin_Install_List_Table::prepare_items()
- WP_Post_Type::set_props()Sets post type properties.
- WP_Taxonomy::set_props()Sets taxonomy properties.
- install_dashboard()Displays the Featured tab of Add Plugins screen.
- sanitize_file_name()Sanitizes a filename, replacing whitespace with dashes.
- wp_privacy_generate_personal_data_export_file()Generate the personal data export file.
- wp_privacy_generate_personal_data_export_group_html()Generate a single group for the personal data export report.
Source code
function sanitize_title_with_dashes( $title, $raw_title = '', $context = 'display' ) { $title = strip_tags( $title ); // Preserve escaped octets. $title = preg_replace( '|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title ); // Remove percent signs that are not part of an octet. $title = str_replace( '%', '', $title ); // Restore octets. $title = preg_replace( '|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title ); if ( seems_utf8( $title ) ) { if ( function_exists( 'mb_strtolower' ) ) { $title = mb_strtolower( $title, 'UTF-8' ); } $title = utf8_uri_encode( $title, 200 ); } $title = strtolower( $title ); if ( 'save' === $context ) { // Convert  , &ndash, and &mdash to hyphens. $title = str_replace( array( '%c2%a0', '%e2%80%93', '%e2%80%94' ), '-', $title ); // Convert  , &ndash, and &mdash HTML entities to hyphens. $title = str_replace( array( ' ', ' ', '–', '–', '—', '—' ), '-', $title ); // Convert forward slash to hyphen. $title = str_replace( '/', '-', $title ); // Strip these characters entirely. $title = str_replace( array( // Soft hyphens. '%c2%ad', // ¡ and ¿. '%c2%a1', '%c2%bf', // Angle quotes. '%c2%ab', '%c2%bb', '%e2%80%b9', '%e2%80%ba', // Curly quotes. '%e2%80%98', '%e2%80%99', '%e2%80%9c', '%e2%80%9d', '%e2%80%9a', '%e2%80%9b', '%e2%80%9e', '%e2%80%9f', // Bullet. '%e2%80%a2', // ©, ®, °, &hellip, and &trade. '%c2%a9', '%c2%ae', '%c2%b0', '%e2%80%a6', '%e2%84%a2', // Acute accents. '%c2%b4', '%cb%8a', '%cc%81', '%cd%81', // Grave accent, macron, caron. '%cc%80', '%cc%84', '%cc%8c', // Non-visible characters that display without a width. '%e2%80%8b', // Zero width space. '%e2%80%8c', // Zero width non-joiner. '%e2%80%8d', // Zero width joiner. '%e2%80%8e', // Left-to-right mark. '%e2%80%8f', // Right-to-left mark. '%e2%80%aa', // Left-to-right embedding. '%e2%80%ab', // Right-to-left embedding. '%e2%80%ac', // Pop directional formatting. '%e2%80%ad', // Left-to-right override. '%e2%80%ae', // Right-to-left override. '%ef%bb%bf', // Byte order mark. '%ef%bf%bc', // Object replacement character. ), '',Changelog
Introduced in 1.2.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 6.8.8 tag, from
src/wp-includes/formatting.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.