_truncate_post_slug

Truncate a post slug.

Description

(string) _truncate_post_slug( (string) $slug, (int) $length = 200 ); 

Returns (string)

The truncated slug.

Parameters (2)

0. $slug (string)
The slug to truncate.
1. $length — Optional. (int) => 200
Max length of the slug. Default 200 (characters).

Usage

  1. if ( !function_exists( '_truncate_post_slug' ) ) { 
  2. require_once ABSPATH . WPINC . '/post.php'; 
  3.  
  4. // The slug to truncate. 
  5. $slug = ''; 
  6.  
  7. // Optional. Max length of the slug. Default 200 (characters). 
  8. $length = 200; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = _truncate_post_slug($slug, $length); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/post.php  
  1. function _truncate_post_slug( $slug, $length = 200 ) { 
  2. if ( strlen( $slug ) > $length ) { 
  3. $decoded_slug = urldecode( $slug ); 
  4. if ( $decoded_slug === $slug ) 
  5. $slug = substr( $slug, 0, $length ); 
  6. else 
  7. $slug = utf8_uri_encode( $decoded_slug, $length ); 
  8.  
  9. return rtrim( $slug, '-' );