url_shorten( string $url, int $length = 35 )

Shorten a URL, to be used as link text.


Description Description


Parameters Parameters

$url

(string) (Required) URL to shorten.

$length

(int) (Optional) Maximum length of the shortened URL. Default 35 characters.

Default value: 35


Top ↑

Return Return

(string) Shortened URL.


Top ↑

Source Source

File: wp-includes/formatting.php

function url_shorten( $url, $length = 35 ) {
	$stripped  = str_replace( array( 'https://', 'http://', 'www.' ), '', $url );
	$short_url = untrailingslashit( $stripped );

	if ( strlen( $short_url ) > $length ) {
		$short_url = substr( $short_url, 0, $length - 3 ) . '…';
	}
	return $short_url;
}

Top ↑

Changelog Changelog

Changelog
Version Description
4.4.0 Moved to wp-includes/formatting.php from wp-admin/includes/misc.php and added $length param.
1.2.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.