_truncate_post_slug( string $slug, int $length = 200 ): string
- Since
- 3.6.0
- Source
wp-includes/post.php:5563
Compatibility
- WordPress
- since 3.6.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
$slugstring- The slug to truncate.
$lengthintoptional- Max length of the slug. Default 200 (characters).Default:
200
Return value
string- The truncated slug.
Performance profile
How much work a call to _truncate_post_slug() 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
- 10–22
- Plugin surface
- None
- Called by
- 3
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 26.
Nothing here hands control to plugin code.
3 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost _truncate_post_slug() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!$length | 10 | rtrim() |
$length && $decoded_slug === false | 20 | urldecode(), rtrim() |
$length && $decoded_slug !== false | 22 | urldecode(), utf8_uri_encode(), rtrim() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 26 | 10–22 | 2 | |
| 8.5 | 26 | 10–22 | 2 | |
| 8.4 | 26 | 10–22 | 2 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 29 | 10–23 | 2 | |
| 8.2 | 29 | 10–23 | 2 | |
| 8.1 | 29 | 10–23 | 2 | |
| 7.4 | 29 | 10–23 | 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 · 1
- utf8_uri_encode()Encodes the Unicode values to be used in the URI.
Used by · 3
- wp_add_trashed_suffix_to_post_name_for_post()Adds a trashed suffix for a given post.
- wp_filter_wp_template_unique_post_slug()Generates a unique slug for templates.
- wp_unique_post_slug()Computes a unique slug for the post, when given the desired slug and some post details.
Source code
function _truncate_post_slug( $slug, $length = 200 ) { if ( strlen( $slug ) > $length ) { $decoded_slug = urldecode( $slug ); if ( $decoded_slug === $slug ) { $slug = substr( $slug, 0, $length ); } else { $slug = utf8_uri_encode( $decoded_slug, $length, true ); } } return rtrim( $slug, '-' );}Changelog
Introduced in 3.6.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/post.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.