the_shortlink() WordPress Function

The shortlink is a URL shortening service for WordPress.com. It uses the WordPress.com servers to create a short URL for a given WordPress.com post or page.

the_shortlink( string $text = '', string $title = '', string $before = '', string $after = '' ) #

Displays the shortlink for a post.


Description

Must be called from inside "The Loop"

Call like the_shortlink( __( ‘Shortlinkage FTW’ ) )


Top ↑

Parameters

$text

(string)(Optional)The link text or HTML to be displayed. Defaults to 'This is the short link.'

Default value: ''

$title

(string)(Optional)The tooltip for the link. Must be sanitized. Defaults to the sanitized post title.

Default value: ''

$before

(string)(Optional)HTML to display before the link.

Default value: ''

$after

(string)(Optional)HTML to display after the link.

Default value: ''


Top ↑

More Information

Used on single post permalink pages, this template tag displays a “URL shortening” link for the current post. By default, this will mean the URL has a format of /?p=1234, and will only appear if pretty permalinks are enabled.

However, this feature is limited by design and intended to be leveraged by plugins that may offer links in a different format, a custom format, or even a format provided by a third-party URL shortening service. Refer to get_permalink() if you want to return the permalink to a post for use in PHP.

Note: This function outputs the complete shortlink for the post, to return the shortlink use wp_get_shortlink().

 


Top ↑

Source

File: wp-includes/link-template.php

function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
	$post = get_post();

	if ( empty( $text ) ) {
		$text = __( 'This is the short link.' );
	}

	if ( empty( $title ) ) {
		$title = the_title_attribute( array( 'echo' => false ) );
	}

	$shortlink = wp_get_shortlink( $post->ID );

	if ( ! empty( $shortlink ) ) {
		$link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>';

		/**
		 * Filters the short link anchor tag for a post.
		 *
		 * @since 3.0.0
		 *
		 * @param string $link      Shortlink anchor tag.
		 * @param string $shortlink Shortlink URL.
		 * @param string $text      Shortlink's text.
		 * @param string $title     Shortlink's title attribute.
		 */
		$link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title );
		echo $before, $link, $after;
	}
}


Top ↑

Changelog

Changelog
VersionDescription
3.0.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More