WordPress at a glance

url_shorten() WP 1.0

Shorten a URL, to be used as link text.

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

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

Code of url_shorten: wp-includes/formatting.php VER 4.9.8

<?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