W3cubDocs

/WordPress

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

Truncates a post slug.

Description

See also

Parameters

$slugstringrequired
The slug to truncate.
$lengthintoptional
Max length of the slug. Default 200 (characters).

Default:200

Return

string The truncated slug.

Source

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

Version Description
3.6.0 Introduced.

© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/_truncate_post_slug