url_shorten

The timeline below displays how wordpress function url_shorten has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Shortens a URL, to be used as link text.
 *
 * @since 1.2.0
 * @since 4.4.0 Moved to wp-includes/formatting.php from wp-admin/includes/misc.php and added $length param.
 *
 * @param string $url    URL to shorten.
 * @param int    $length Optional. Maximum length of the shortened URL. Default 35 characters.
 * @return string Shortened URL.
 */
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;
}

WordPress Version: 4.5

/**
 * Shorten a URL, to be used as link text.
 *
 * @since 1.2.0
 * @since 4.4.0 Moved to wp-includes/formatting.php from wp-admin/includes/misc.php and added $length param.
 *
 * @param string $url    URL to shorten.
 * @param int    $length Optional. Maximum length of the shortened URL. Default 35 characters.
 * @return string Shortened URL.
 */
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;
}

WordPress Version: 4.4

/**
 * Shorten an URL, to be used as link text.
 *
 * @since 1.2.0
 * @since 4.4.0 Moved to wp-includes/formatting.php from wp-admin/includes/misc.php and added $length param.
 *
 * @param string $url    URL to shorten.
 * @param int    $length Optional. Maximum length of the shortened URL. Default 35 characters.
 * @return string Shortened URL.
 */
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;
}

WordPress Version: 3.8

/**
 * Shorten an URL, to be used as link text
 *
 * @since 1.2.0
 *
 * @param string $url
 * @return string
 */
function url_shorten($url)
{
    $short_url = str_replace(array('http://', 'www.'), '', $url);
    $short_url = untrailingslashit($short_url);
    if (strlen($short_url) > 35) {
        $short_url = substr($short_url, 0, 32) . '…';
    }
    return $short_url;
}

WordPress Version: 3.7

/**
 * Shorten an URL, to be used as link text
 *
 * @since 1.2.1
 *
 * @param string $url
 * @return string
 */
function url_shorten($url)
{
    $short_url = str_replace(array('http://', 'www.'), '', $url);
    $short_url = untrailingslashit($short_url);
    if (strlen($short_url) > 35) {
        $short_url = substr($short_url, 0, 32) . '…';
    }
    return $short_url;
}