url_shorten

Shorten a URL, to be used as link text.

Description

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

Returns (string)

Shortened URL.

Parameters (2)

0. $url (string)
URL to shorten.
1. $length — Optional. (int) => 35
Maximum length of the shortened URL. Default 35 characters.

Usage

  1. if ( !function_exists( 'url_shorten' ) ) { 
  2. require_once ABSPATH . WPINC . '/formatting.php'; 
  3.  
  4. // URL to shorten. 
  5. $url = ''; 
  6.  
  7. // Optional. Maximum length of the shortened URL. Default 35 characters. 
  8. $length = 35; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = url_shorten($url, $length); 
  12.  

Defined (1)

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

/wp-includes/formatting.php  
  1. function url_shorten( $url, $length = 35 ) { 
  2. $stripped = str_replace( array( 'https://', 'http://', 'www.' ), '', $url ); 
  3. $short_url = untrailingslashit( $stripped ); 
  4.  
  5. if ( strlen( $short_url ) > $length ) { 
  6. $short_url = substr( $short_url, 0, $length - 3 ) . '…'; 
  7. return $short_url;