WordPress at a glance

url_shorten() WP 1.0

Shorten a URL, to be used as link text.

No Hooks.

Return

String. Shortened URL.

Usage

url_shorten( $url, $length );
$url(string) (required)
URL to shorten.
$length(int)
Maximum length of the shortened URL.
Default: 35 characters

Changelog

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

Code of url shorten: wp-includes/formatting.php WP 5.2.2

<?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 ) . '&hellip;';
	}
	return $short_url;
}

Related Functions

From category: Formatting

No comments
    Hello, !     Log In . Register